在哪个时间点在图表中初始化系列颜色 [英] At which point in time is the series colour initialised in a chart graph

查看:108
本文介绍了在哪个时间点在图表中初始化系列颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Chart绘制2条线的图形.现在,我的目标是将第二个Y轴的MajorGridLineColor设置为相应线条的颜色.这是我的代码:

I use Chart to draw a graph with 2 lines. Now my aim is to set the LineColor of the MajorGrid of the second Y-axis to the color of the corresponding line. Here is my code:

public partial class Form1 : Form
{

    List<double> values_1 = new List<double>();
    List<double> values_2 = new List<double>();

    public Form1()
    {
        InitializeComponent();

        make_values();

        for (int i = 0; i < values_1.Count; i++)
        {
            chart1.Series[0].Points.AddY(values_1[i]);
        }

        for (int i = 0; i < values_2.Count; i++)
        {
            chart1.Series[1].Points.AddY(values_2[i]);
        }

        // set the colour of grid to corresponding line
        chart1.ChartAreas[0].AxisY2.MajorGrid.LineColor = chart1.Series[1].Color;

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void make_values()
    {
        for (int i = 0; i < 600; i++)
        {
            values_1.Add(Math.Sin(i / 60.0));
            values_2.Add(Math.Cos(i / 60.0));
        }

    }
}

由于自动为2个不同的系列选择了颜色,尽管我可以抓住颜色.但是在调试时,我看到颜色是(0,0,0):

Since the colours are chosen automatically for the 2 different series I though I could just grab the colour. But when debugging I see that the colour is (0,0,0):

因此,网格颜色不会更改.但是第二个系列的颜色不是(0,0,0),如加载窗口时所见!:

So the grid colour does not change. But the colour of the second series is not (0,0,0) as can be seen when the window is loaded!:

如果在此之前我强制并手动设置2系列的颜色.一切正常,并且网格获得了相应的颜色.

If I force and set manually the colours of the 2 series before that. Everything works fine, and the grid gets the corresponding colour.

有人知道在什么时候我必须抓住系列的颜色才能获得真正的价值?

Does anyone know at which point in time I would have to grab the colour of the series to get the real value?

推荐答案

要访问系列颜色,您需要调用ApplyPaletteColors.当您想将它们用于其他元素或自定义工程图时,这是必需的.您还应该在更改

To access the Series Colors you need to call ApplyPaletteColors. This is necessary when you want to use them for other elements or when custom drawing. You should also call it again after changing the palette..

chart1.ApplyPaletteColors();

MSDN :

备注

在运行时自动分配图表颜色时, 没有办法知道什么时候之前的颜色是什么 图表绘制;自动分配值的Color属性 此时将返回空".

When the Chart colors are automatically assigned at run time, there is no way to know what the colors will be prior to the time when the chart rendered; the Color property of an automatically assigned value will return Empty at this time.

如果调用ApplyPaletteColors方法,则该系列的颜色 数据点将被设置,从而可以进行程序访问.

If you call the ApplyPaletteColors method, the colors for the series and the data points will be set, which allows for programmatic access.

这篇关于在哪个时间点在图表中初始化系列颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆