当前位置:   article > 正文

WPF下实现堆积柱状图_wpf连体堆积柱形图

wpf连体堆积柱形图

VS2010下

准备依赖文件:

System.Windows.Forms.dll;

System.Windows.Forms.DataVisualization.Charting.dll;

WindowsFormIntegration.dll

MainWindow.xaml编辑如下

  1. <Window x:Class="MainWindow.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
  5. xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
  6. xmlns:wfc="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
  7. Title="WPF Chart Demo" Width="903" Height="581">
  8. <Grid Name="grid1">
  9. <WindowsFormsHost Margin="0,47,0,0">
  10. <wfc:Chart Name="Chart1"/>
  11. </WindowsFormsHost>
  12. </Grid>
  13. </Window>

MainWindow.xaml.cs代码如下

  1. private void button1_Click(object sender, RoutedEventArgs e)
  2. {
  3. addData();
  4. }
  5. private void addData() {
  6. var windowsFormsHost = (WindowsFormsHost)grid1.Children[0];
  7. var chart = (Chart)windowsFormsHost.Child;
  8. chart.Series.Clear();
  9. chart.ChartAreas.Clear();
  10. // ChartArea追加
  11. chart.ChartAreas.Add("ChartArea1");
  12. chart.ChartAreas[0].AxisX.Interval = 1;
  13. chart.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;
  14. chart.ChartAreas[0].BorderColor =System.Drawing.Color.Red;
  15. //series的制作和值的追加
  16. Series seriesSin = new Series();
  17. seriesSin.ChartType = SeriesChartType.StackedColumn;
  18. seriesSin.MarkerStyle = MarkerStyle.Circle;
  19. Series seriesCos = new Series();
  20. seriesCos.ChartType = SeriesChartType.StackedColumn;
  21. seriesCos.MarkerStyle = MarkerStyle.Circle;
  22. string[] xValues = new string[] { "A", "B", "C", "D", "E"}; //x轴的数据
  23. int[,] yValues = new int[,] { { 10, 20, 30, 40, 50 }, { 20, 40, 60, 80, 100 } }; //y轴的数据
  24. for (int x = 1; x <= xValues.Length; x++)
  25. {
  26. seriesCos.IsValueShownAsLabel = true;
  27. seriesSin.IsValueShownAsLabel = true;
  28. seriesSin.Points.AddXY(x, x);
  29. seriesCos.Points.AddXY(x, x);
  30. }
  31. chart.Series.Add(seriesSin);
  32. chart.Series.Add(seriesCos);
  33. }

效果:

上述的

seriesSin.ChartType = SeriesChartType.StackedColumn;

可以设置各种的统计图表如:


更具上诉代码就可以实现;

项目和相关dll:下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/270769
推荐阅读
相关标签
  

闽ICP备14008679号