日期:2014-05-18  浏览次数:21628 次

vs2010 c# 如何用chart绘制折线图,
默认的是柱状图,

想绘制折线图,百度、google查到的几乎都是柱状图、饼图,如何绘制折线图呢?

------解决方案--------------------

先循环把点加进去
for()
{
Chart1.Series["aaaa"].Points.AddXY(横坐标的值,纵坐标的值);
}
设置图表类型
Chart1.Series["aaaa"].ChartType=SeriesChartType.****
SeriesChartType是个枚举,包含很多图表类型,有柱状图,饼状图,自己看看就行了。


------解决方案--------------------
下面是我写过的,记录过车重量变化的折线图,用的是第三方的XtraChart控件

C# code

            if (chartControlCar.Series == null)
                return;
            chartControlCar.DataSource = null;
            chartControlCar.SeriesDataMember = null;
            chartControlCar.Series.Clear();

            Series S1 = new Series("",ViewType.Line);  //定义该折线的名称和类型

            Legend lg = chartControlCar.Legend;  //不显示折线名称
            lg.Visible = false;

            S1.ArgumentScaleType = ScaleType.Numerical;  //X轴为数值型
            S1.ValueScaleType = ScaleType.Numerical;  //Y轴为数值型

            ((LineSeriesView)S1.View).LineMarkerOptions.Visible = false; //不显示折线节点
            PointSeriesLabel label = (PointSeriesLabel)S1.Label;
            label.Visible = false; //不显示节点数值
            
            switch (strLineStyle) //设置线条类型
            {
                case "DASH":
                    ((LineSeriesView)S1.View).LineStyle.DashStyle = DashStyle.Dash;
                    break;
                case "DOT":
                    ((LineSeriesView)S1.View).LineStyle.DashStyle = DashStyle.Dot;
                    break;
                case "DASHDOT":
                    ((LineSeriesView)S1.View).LineStyle.DashStyle = DashStyle.DashDot;
                    break;
                case "SOLID":
                    ((LineSeriesView)S1.View).LineStyle.DashStyle = DashStyle.Solid;
                    break;
                default:
                    ((LineSeriesView)S1.View).LineStyle.DashStyle = DashStyle.Solid;
                    break;
            }
           
            S1.PointOptions.PointView = PointView.Argument;

             for(int i=0;i<strArrayWeights.Length;i++) 
            {
                 //X轴为二次表数值显示变化次数,Y轴为重量值
                S1.Points.Add(new SeriesPoint(i + 1, new double[] { double.Parse(strArrayWeights[i]) }));
            }
            
            chartControlCar.Series.Add(S1);

            XYDiagram diagram = (XYDiagram)chartControlCar.Diagram;
            diagram.AxisY.Range.MaxValue = intAxisYMaxValue;  //设置Y轴显示最大值
            diagram.AxisY.Range.MinValue = intAxisYMinValue;  //设置Y轴显示最小值