参考:
Devexpress Charts动态添加数据:
https://www.cnblogs.com/zhangruisoldier/p/4226950.html
DevExpress 图表控件:ChartControl 动态绑定数据
https://blog.csdn.net/keenweiwei/article/details/7432314
效果图:
代码:
using System.Collections.Generic;
using DevExpress.XtraCharts;
namespace WinFormCharts
{
public partial class FrmMain : DevExpress.XtraEditors.XtraForm
{
private Dictionary<string, double> chartPieDataDic = new Dictionary<string, double>();
private Dictionary<string, double> chartBarDataDic = new Dictionary<string, double>();
public FrmMain()
{
InitializeComponent();
InitPieChartData();
InitBarChartData();
showPie();
showPoint();
showBar();
showLine();
}
/// <summary>
/// 扇形图
/// </summary>
private void showPie()
{
ChartTitle title = new ChartTitle();
title.Text = "全省话务分析饼图-话务类型分布";
chartControl1.Titles.Clear();
chartControl1.Titles.Add(title);
Series series1 = new Series("话务类型", ViewType.Pie);
((PiePointOptions)series1.PointOptions).PointView = PointView.ArgumentAndValues;
foreach (KeyValuePair<string, double> keyvalue in chartPieDataDic)
{
series1.Points.Add(new SeriesPoint(keyvalue.Key, new double[] { keyvalue.Value }));
}
chartControl1.Series.Clear();
chartControl1.Series.Add(series1);
}
/// <summary>
/// 柱状图
/// </summary>
private void showBar()
{
try
{
ChartTitle title = new ChartTitle();
title.Text = "市网元容量增长率统计分析柱图";
chartControl2.Titles.Clear();
chartControl2.Titles.Add(title);
Series series2 = new Series("大连", ViewType.Bar);
Series series3 = new Series("沈阳", ViewType.Bar);
Series series4 = new Series("本溪", ViewType.Bar);
foreach (KeyValuePair<string, double> kvp in chartBarDataDic)
{
series2.Points.Add(new SeriesPoint(kvp.Key, new double[] { kvp.Value }));
series3.Points.Add(new SeriesPoint(kvp.Key, new double[] { kvp.Value + 0.08 }));
series4.Points.Add(new SeriesPoint(kvp.Key, new double[] { kvp.Value + 0.02 }));
}
chartControl2.Series.Clear();
chartControl2.Series.Add(series2);
chartControl2.Series.Add(series3);
chartControl2.Series.Add(series4);
chartControl2.Legend.Visible = true; // 显示标签(旁边的标注哪个颜色代表哪个城市)
}
catch { }
}
/// <summary>
/// 折线图
/// </summary>
private void showLine()
{
ChartTitle title = new ChartTitle();
title.Text = "线图";
chartControl3.Titles.Clear();
chartControl3.Titles.Add(title);
Series series1 = new Series("辽宁", ViewType.Line);
series1.ArgumentScaleType = ScaleType.Numerical;
series1.Points.Add(new SeriesPoint(1, new double[] { 10 }));
series1.Points.Add(new SeriesPoint(2, new double[] { 12 }));
series1.Points.Add(new SeriesPoint(3, new double[] { 14 }));
series1.Points.Add(new SeriesPoint(4, new double[] { 17 }));
series1.Points.Add(new SeriesPoint(5, new double[] { 21 }));
series1.Points.Add(new SeriesPoint(6, new double[] { 26 }));
series1.Points.Add(new SeriesPoint(7, new double[] { 29 }));
series1.Points.Add(new SeriesPoint(8, new double[] { 30 }));
((PointSeriesView)series1.View).PointMarkerOptions.Kind = MarkerKind.Triangle;
Series series2 = new Series("山东", ViewType.Line);
series2.ArgumentScaleType = ScaleType.Numerical; //这句话必须有,否则点画不出来.
((LineSeriesView)series2.View).LineStyle.DashStyle = DashStyle.DashDotDot;
series2.Points.Add(new SeriesPoint(1, new double[] { 4 }));
series2.Points.Add(new SeriesPoint(2, new double[] { 14 }));
series2.Points.Add(new SeriesPoint(3, new double[] { 17 }));
series2.Points.Add(new SeriesPoint(4, new double[] { 22 }));
series2.Points.Add(new SeriesPoint(5, new double[] { 20 }));
series2.Points.Add(new SeriesPoint(6, new double[] { 15 }));
series2.Points.Add(new SeriesPoint(7, new double[] { 18 }));
series2.Points.Add(new SeriesPoint(8, new double[] { 11 }));
((PointSeriesView)series2.View).PointMarkerOptions.Kind = MarkerKind.Cross;
chartControl3.Series.Clear();
chartControl3.Series.Add(series1);
chartControl3.Series.Add(series2);
chartControl3.Legend.Visible = true;
}
private void InitPieChartData()
{
try
{
chartPieDataDic.Add("大连", 0.2);
chartPieDataDic.Add("沈阳", 0.26);
chartPieDataDic.Add("锦州", 0.15);
chartPieDataDic.Add("盘锦", 0.15);
chartPieDataDic.Add("本溪", 0.1);
chartPieDataDic.Add("抚顺", 0.12);
chartPieDataDic.Add("营口", 0.02);
}
catch { }
}
private void InitBarChartData()
{
try
{
chartBarDataDic.Add("2001", 0.05);
chartBarDataDic.Add("2002", 0.02);
chartBarDataDic.Add("2003", 0.08);
chartBarDataDic.Add("2004", 0.15);
chartBarDataDic.Add("2005", 0.1);
chartBarDataDic.Add("2006", 0.3);
chartBarDataDic.Add("2007", 0.2);
chartBarDataDic.Add("2008", 0.1);
}
catch { }
}
/// <summary>
/// 点状图
/// </summary>
private void showPoint()
{
ChartTitle title = new ChartTitle();
title.Text = "点图";
chartControl4.Titles.Clear();
chartControl4.Titles.Add(title);
Series series1 = new Series("上线人数", ViewType.Point);
series1.ArgumentScaleType = ScaleType.Numerical;
series1.Points.Add(new SeriesPoint(1, new double[] { 20 }));
series1.Points.Add(new SeriesPoint(2, new double[] { 12 }));
series1.Points.Add(new SeriesPoint(3, new double[] { 11 }));
series1.Points.Add(new SeriesPoint(4, new double[] { 17 }));
series1.Points.Add(new SeriesPoint(5, new double[] { 21 }));
series1.Points.Add(new SeriesPoint(6, new double[] { 21 }));
series1.Points.Add(new SeriesPoint(7, new double[] { 29 }));
series1.Points.Add(new SeriesPoint(8, new double[] { 30 }));
((PointSeriesView)series1.View).PointMarkerOptions.Kind = MarkerKind.Triangle;
Series series2 = new Series("下线人数", ViewType.Point);
series2.ArgumentScaleType = ScaleType.Numerical; //这句话必须有,否则点画不出来.
series2.Points.Add(new SeriesPoint(1, new double[] { 4 }));
series2.Points.Add(new SeriesPoint(2, new double[] { 14 }));
series2.Points.Add(new SeriesPoint(3, new double[] { 17 }));
series2.Points.Add(new SeriesPoint(4, new double[] { 22 }));
series2.Points.Add(new SeriesPoint(5, new double[] { 20 }));
series2.Points.Add(new SeriesPoint(6, new double[] { 15 }));
series2.Points.Add(new SeriesPoint(7, new double[] { 18 }));
series2.Points.Add(new SeriesPoint(8, new double[] { 11 }));
((PointSeriesView)series2.View).PointMarkerOptions.Kind = MarkerKind.Cross;
chartControl4.Series.Clear();
chartControl4.Series.Add(series1);
chartControl4.Series.Add(series2);
chartControl4.Legend.Visible = true;
}
}
}