当前位置:   article > 正文

WPFTookit Chart 入门

wpf toolkit chart

如何使用WPFToolKit Chart

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. var s = new Series();
  4. s.ChartType = SeriesChartType.Line;
  5. var d = new DateTime(2013, 04, 01);
  6. s.Points.AddXY(d, 3);
  7. s.Points.AddXY(d.AddMonths(-1), 2);
  8. s.Points.AddXY(d.AddMonths(-2), 1);
  9. s.Points.AddXY(d.AddMonths(-3), 4);
  10. chart1.Series.Clear();
  11. chart1.Series.Add(s);
  12. chart1.Series[0].XValueType = ChartValueType.DateTime;
  13. chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
  14. chart1.ChartAreas[0].AxisX.Interval = 1;
  15. chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
  16. chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
  17. chart1.Series[0].XValueType = ChartValueType.DateTime;
  18. DateTime minDate = new DateTime(2013, 01, 01).AddSeconds(-1);
  19. DateTime maxDate = new DateTime(2013, 05, 01); // or DateTime.Now;
  20. chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
  21. chart1.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
  22. }

 

或者

  1. <charting:Chart x:Name="chtSummary" Width="770" Height="400" Title="My Chart Title">
  2. <charting:Chart.TitleStyle>
  3. <Style TargetType="datavis:Title">
  4. <Setter Property="FontSize" Value="28" />
  5. <Setter Property="FontFamily" Value="Arial" />
  6. <Setter Property="Margin" Value="5, 10, 5, 15" />
  7. </Style> </charting:Chart.TitleStyle>
  8. <charting:Chart.LegendStyle>
  9. <Style TargetType="datavis:Legend">
  10. <Setter Property="Width" Value="0" />
  11. </Style> </charting:Chart.LegendStyle>
  12. <charting:Chart.Series>
  13. <charting:BarSeries ItemsSource="{Binding}" DependentValuePath="Value" IndependentValuePath="Key" IsSelectionEnabled="True" >
  14. <charting:BarSeries.IndependentAxis>
  15. <charting:CategoryAxis Orientation="Y" AxisLabelStyle="{StaticResource SummaryChartAxisStyle}" />
  16. </charting:BarSeries.IndependentAxis>
  17. </charting:BarSeries>
  18. </charting:Chart.Series>
  19. </charting:Chart>
  20. <!-- See more at: http://mitchelsellers.com/blogs/2011/04/20/wpf-chart-styling-explained.aspx#sthash.EdMcBh2I.dpuf-->

 

隐藏Legend

隐藏Chart的Legend

  1. <chartingToolkit:Chart.LegendStyle>
  2. <Style TargetType="Control">
  3. <Setter Property="Width" Value="0" />
  4. <Setter Property="Height" Value="0" />
  5. </Style>
  6. </chartingToolkit:Chart.LegendStyle>

 

隐藏具体Item的legend

  1. <charting:LineSeries.LegendItemStyle >
  2. <Style TargetType="{x:Type charting:LegendItem}">
  3. <Setter Property="Visibility" Value="Collapsed"/>
  4. </Style>
  5. </charting:LineSeries.LegendItemStyle>

 

显示定义横纵坐标

  1. <Grid Height="800">
  2. <chartingToolkit:Chart Name="lineChart" Title="Pressure over Time"
  3. VerticalAlignment="Top" Margin="20,50,20,0" Height="500">
  4. <chartingToolkit:Chart.Axes>
  5. <chartingToolkit:LinearAxis Title="Pressure" Orientation="Y" Interval="100" />
  6. <chartingToolkit:LinearAxis Title="Time" Orientation="X" Interval="100" />
  7. </chartingToolkit:Chart.Axes>
  8. <chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" Name="Test"
  9. IsSelectionEnabled="True" ClipToBounds="False">
  10. </chartingToolkit:LineSeries> </chartingToolkit:Chart>
  11. <Button Width="100" Height="24" Margin="20,556,1058,220" Content="More" Name="Button1" />
  12. </Grid>

 

修改横纵坐标

 

坐标值倒序排列

Complete success

 

 

  1. public class InverterConverter : IValueConverter
  2. {
  3. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  4. {
  5. if (value is int)
  6. {
  7. return -(int)value;
  8. }
  9. throw new NotImplementedException();
  10. }
  11. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  12. {
  13. throw new NotImplementedException();
  14. }
  15. }
 
  1. <charting:Chart
  2. FontSize="9">
  3. <charting:LineSeries
  4. ItemsSource="{Binding}"
  5. DependentValueBinding="{Binding Place, Converter={StaticResource InverterConverter}}"
  6. IndependentValuePath="Date"
  7. Title="Book">
  8. <charting:LineSeries.DataPointStyle>
  9. <Style TargetType="charting:LineDataPoint">
  10. <Setter Property="Background" Value="Maroon"/>
  11. <Setter Property="DependentValueStringFormat" Value="{}{0:0.#;0.#}"/>
  12. </Style>
  13. </charting:LineSeries.DataPointStyle>
  14. <charting:LineSeries.DependentRangeAxis>
  15. <charting:LinearAxis
  16. Orientation="Y"
  17. Minimum="-10.5"
  18. Maximum="-0.5"
  19. Interval="1"
  20. ShowGridLines="True">
  21. <charting:LinearAxis.AxisLabelStyle>
  22. <Style TargetType="charting:AxisLabel">
  23. <Setter Property="StringFormat" Value="{}{0:0.#;0.#}"/>
  24. </Style>
  25. </charting:LinearAxis.AxisLabelStyle>
  26. </charting:LinearAxis>
  27. </charting:LineSeries.DependentRangeAxis>
  28. </charting:LineSeries>
  29. </charting:Chart>

参考 http://dlaa.me/blog/post/9607895

 

修改ToolTips

  1. <ToolTipService.ToolTip>
  2. <StackPanel>
  3. <ContentControl
  4. Content="Custom ToolTip"
  5. FontWeight="Bold"/>
  6. <ContentControl
  7. Content="{TemplateBinding FormattedDependentValue}"/>
  8. </StackPanel>
  9. </ToolTipService.ToolTip>

http://dlaa.me/blog/post/9631686

 

参考

My new home page, extended [Updated collection of great Silverlight and WPF Charting resources!]

转载于:https://www.cnblogs.com/HQFZ/p/4446727.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号