用彩色线条绘制图表

时间:2014-05-06 20:29:22

标签: c# charts drawing

我在C#WinForms应用程序中使用图表组件。我可以绘制很多系列,但所有的线都是相同的颜色。在图表中我只有一行,然后出现颜色。

以下是我用来为特定系列设置新点的代码

 private void SetChart(string title, double x, double y)
    {
        //if (y > 1000) return;
        if (this.chart.InvokeRequired)
        {
            this.chart.Invoke(new MethodInvoker(delegate
            {
                try
                {
                    chart.Series[title].Points.AddXY(x, y);
                    chart.Update();
                }
                catch
                {
                    chart.Series.Add(title);
                    chart.Series[title].Color = System.Drawing.Color.Red;
                    chart.Series[title].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                    chart.Series[title].Points.AddXY(x, y);
                    chart.Update();
                }
            }));
        }
        else
        {
            try
            {
                chart.Series[title].Points.AddXY(x, y);
                chart.Update();
            }
            catch
            {
                chart.Series.Add(title);
                chart.Series[title].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart.Series[title].Points.AddXY(x, y);
                chart.ChartAreas[0].AxisX.Title = LearnByError.Internazional.Resource.Inst.Get("r57");
                chart.ChartAreas[0].AxisX.TitleForeColor = System.Drawing.Color.Black;
                chart.ChartAreas[0].AxisX.Minimum = 1;
                chart.ChartAreas[0].AxisY.Title = LearnByError.Internazional.Resource.Inst.Get("r56");
                chart.ChartAreas[0].AxisY.TitleForeColor = System.Drawing.Color.Black;
                chart.Update();
            }
        }
    }

此代码以下面显示的方式使用,允许我绘制:

 foreach (var weights in lr.ResultWeights)
            {
                String name = LearnByError.Internazional.Resource.Inst.Get("r53") + trial.ToString();
                weightsList.Items.Add(name);
                for (int i = 0; i < weights.Length; i++)
                {
                    weightsList.Items.Add(weights[i].ToString());
                }
                weightsList.Items.Add("");

                if (lr.Info.np == 0) throw new Exception(LearnByError.Internazional.Resource.Inst.Get("r54"));
                foreach (var rmse in lr.RMSE)
                {
                    for (int j = 0; j < rmse.Length; j++)
                    {
                        SetChart(name, j + 1, rmse[j]);                           
                    }
                }
                trial++;
            }

如果有人有这样的问题,我很乐意发现......

图表的图像 enter image description here

1 个答案:

答案 0 :(得分:1)

如果InvokeRequired为假,则永远不会设置系列的颜色。并且,如果InvokeRequired为真,则始终将颜色设置为红色,而不是将它们全部设置为不同的颜色。