WPF Toolkit Chartの折れ線グラフの見栄えを改善したい。
質問1~4の方法について教えてください。
[質問1]
横軸の軸ラベル(時刻の文字列)を時計回りに90度回転して、横幅の不足による見栄えの悪化を解消したい。10分刻みで表示させると、上下二段に表示されてしまい見栄えが悪化する。
[質問2]
横軸の軸ラベル(時刻の文字列)の書式を(a)から(b)に変更したい。
(a) "0:00","0:10","0:20", ... "0:50","1:00" (現行はHourの桁数が変化)
(b) "00:00","00:10","00:20", ... "00:50","01:00" (希望はHourが常時2桁)
[質問3]
垂直方向の目盛線(線色=灰色、太さ=1)を2014年4月1日午前0時0分を起点として30分刻みで表示したい。
[質問4]
水平方向の目盛線(線色=灰色、太さ=1)を0を起点として50刻みで表示したい。
以下、サンプルコード
[XAML]
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="WpfApplication1.MainWindow" Title="MainWindow" Width="500" Height="600"><Window.Resources><chartingToolkit:LinearAxis x:Key="AxisY" Orientation="Y" Minimum="-20" Maximum="200" Interval="20"/><chartingToolkit:DateTimeAxis x:Key="AxisX" Orientation="X" Minimum="{Binding StartTime,Mode=OneWay}" Maximum="{Binding EndTime,Mode=OneWay}" IntervalType="Minutes" Interval="10"/><Style TargetType="Polyline" x:Key="Polyline1"><Setter Property="StrokeThickness" Value="1"/></Style></Window.Resources><Grid><chartingToolkit:Chart Title="測定値の変動" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" Height="400"><chartingToolkit:LineSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding Items,Mode=OneWay}" DependentRangeAxis="{StaticResource ResourceKey=AxisY}" IndependentAxis="{DynamicResource ResourceKey=AxisX}" PolylineStyle="{StaticResource ResourceKey=Polyline1}" Title="測定値"><chartingToolkit:LineSeries.DataPointStyle><Style TargetType="Control"><Setter Property="Background" Value="Blue"/><Setter Property="Template" Value="{x:Null}"/></Style></chartingToolkit:LineSeries.DataPointStyle></chartingToolkit:LineSeries></chartingToolkit:Chart></Grid></Window>
[C#]
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<PointXy> list = new List<PointXy>();
list.Add(new PointXy(new DateTime(2014, 3, 31, 23, 50, 0), 110));
list.Add(new PointXy(new DateTime(2014, 4, 1, 0, 0, 0), 100));
list.Add(new PointXy(new DateTime(2014, 4, 1, 0, 10, 0), 110));
list.Add(new PointXy(new DateTime(2014, 4, 1, 0, 20, 0), 120));
list.Add(new PointXy(new DateTime(2014, 4, 1, 0, 30, 0), 130));
list.Add(new PointXy(new DateTime(2014, 4, 1, 0, 40, 0), 140));
list.Add(new PointXy(new DateTime(2014, 4, 1, 0, 50, 0), 150));
list.Add(new PointXy(new DateTime(2014, 4, 1, 1, 0, 0), 140));
list.Add(new PointXy(new DateTime(2014, 4, 1, 1, 10, 0), 130));
list.Add(new PointXy(new DateTime(2014, 4, 1, 1, 20, 0), 120));
list.Add(new PointXy(new DateTime(2014, 4, 1, 1, 30, 0), 110));
list.Add(new PointXy(new DateTime(2014, 4, 1, 1, 40, 0), 100));
list.Add(new PointXy(new DateTime(2014, 4, 1, 1, 50, 0), 90));
list.Add(new PointXy(new DateTime(2014, 4, 1, 2, 0, 0), 80));
list.Add(new PointXy(new DateTime(2014, 4, 1, 2, 10, 0), 70));
list.Add(new PointXy(new DateTime(2014, 4, 1, 2, 10, 0), 60));
_myData = new MyData();
_myData.Update(list);
this.DataContext = _myData;
}
private MyData _myData;
}
public class PointXy : INotifyPropertyChanged
{
public PointXy(DateTime x, double y) { _x = x; _y = y; }
public DateTime X
{
get { return _x; }
set
{
_x = value;
OnPropertyChanged("X");
}
}
public double Y
{
get { return _y; }
set
{
_y = value;
OnPropertyChanged("Y");
}
}
private DateTime _x;
private double _y;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class MyData : INotifyPropertyChanged
{
public MyData()
{
_items = new ObservableCollection<PointXy>();
}
public void Update(List<PointXy> items)
{
_items.Clear();
foreach (PointXy item in items)
{
_items.Add(item);
}
OnPropertyChanged("Items");
OnPropertyChanged("StartTime");
OnPropertyChanged("EndTime");
}
private ObservableCollection<PointXy> _items;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<PointXy> Items { get { return _items; } }
public DateTime StartTime { get { return _items.First().X; } }
public DateTime EndTime { get { return _items.Last().X; } }
}
}
[描画結果]
![]()
C#開発者