using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.DataVisualization.Charting;
namespace Chartdata
{
internal class Super_chart
{
private Chart chart=null;
public Super_chart(Chart data )
{
this.chart = chart;
}
/// <summary>
/// 绘制图表的通用方法
/// </summary>
/// <param name="chartType">图表显示的类型</param>
/// <param name="datalist">图表所需要的数据</param>
public void showChart(SeriesChartType chartType, List<chart_data>datalist)
{
//【1】清除所有的图表序列
this.chart .Series.Clear();
//【2】创建一个图表序列对象(一个图表,可以添加多个图表序列,也就是绘图对象)
Series series1=new Series();
//设置图表序列对象显示的类型
series1.ChartType=chartType;
//添加图表序列集合
this.chart .Series.Add(series1);
//【3】设置当前图表序列的各种属性值
for (int i = 0; i < datalist.Count; i++)
{
//获取数据对象的两个值
string text =datalist[i].Text;
double value=datalist[i].Value;
//使用x和y的值将DatePoint对象添加进去
series1.Points.AddXY(text, value);
//设置数据点显示内容
//鼠标放到标签上的提示
series1.Points[i].LabelToolTip=value.ToString();
//鼠标放到图形上的提示
series1.Points[i].ToolTip=value.ToString();
//根据图形样式设置显示的形式和内容
if (chartType == SeriesChartType.Pie)//饼形图
{
//设置标签显示的内容=x轴内容+value
series1.Points[i].Label = "#AXISLABEL(#VAL)";
//x轴+百分比
series1.Points[i].Label = "#AXISLABEL(#PERCENT)";
series1.Points[i].Label = "#AXISLABEL(#VAL)(#PERCENR)";
//在外侧显示lable
series1["PieLabelStyle"] = "Outside";
//在内存显示label
// series1["PieLabelStyle"] = "Inside";
//绘制连线
series1["PieLineColor"] = "Black";
}
else if (chartType == SeriesChartType.Doughnut)//圆环图
{
series1.Points[i].Label = "#AXISLABEL (#PERCENT)";
//在内部显示label
series1["PieLabelStyle"] = "Inside";
}
else //其他图像,显示百分比或者数值
{
series1.Points[i].Label = "#AXISLABEL (#PERCENT)";
series1.Points[i].Label = "#AXISLABEL(#VAL)";
}
if (chartType != SeriesChartType.Pie)
{
series1.Points[i].AxisLabel=string.Format("{0}{1}",text,value);
}
}
//【4】设置图表绘图区域的x和y的坐标值(y表示具体要显示的数值之间的间隔)
//也可以设置成为20
this.chart.ChartAreas[0].AxisY.Interval = 10;
//一般这里设置为1
this.chart.ChartAreas[0].AxisX.Interval = 1;
}
}
}