MS chart candlestick How to set tail colors

随声附和 提交于 2019-12-11 03:38:03

问题


I am currently developing a candlestick chart with mschart in visual C#. I have now created two charts and created the charts as follows

Question 1. View the Candlestick Chart at the top. I would like to apply the tail color of each rod as red or blue.

Question 2. View the bar chart at the bottom. I would like to apply Red or Blue color to this chart. I want to apply the same color to the top of the Candlestick chart. How can I do it ?

[source]

DataTable table_ChartData = new DataTable();
table_ChartData.Columns.Add("Id");
table_ChartData.Columns.Add("Open");
table_ChartData.Columns.Add("Close");
table_ChartData.Columns.Add("High");
table_ChartData.Columns.Add("Low");
table_ChartData.Columns.Add("Day");
dataGridView1.DataSource = table_ChartData;  

chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.Maximum = max;
chart1.ChartAreas["ChartArea1"].AxisY.Minimum = min;

chart1.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;
chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = true;


chart1.Series["Candle"].XValueMember = "Day";            
chart1.Series["Candle"].YValueMembers = "High,Low,Open,Close,Volume";
chart1.Series["Candle"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chart1.Series["Candle"].CustomProperties = "PriceDownColor=Blue,PriceUpColor=Red";
chart1.Series["Candle"]["OpenCloseStyle"] = "Triangle";
chart1.Series["Candle"]["ShowOpenClose"] = "Both";
chart1.DataSource = table_ChartData;
chart1.DataBind();

////////////////////////////////////////////////////////////

chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart2.ChartAreas["ChartArea1"].AxisY.Maximum = v_max + (v_max / 10);
chart2.ChartAreas["ChartArea1"].AxisY.Minimum = v_min / 2;

chart2.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
chart2.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;
chart2.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = true;


chart2.Series["Bar"].XValueMember = "Day";
chart2.Series["Bar"].YValueMembers = "Volume";
chart2.Series["Bar"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chart2.Series["Bar"].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
chart2.DataSource = table_ChartData;
chart2.DataBind();


回答1:


In a Candlestick Chart there are CustomProperties to automatiaclly set the colors of the boxes, depending on the trend:

someSeries.SetCustomProperty("PriceUpColor", "Green");   
someSeries.SetCustomProperty("PriceDownColor", "Red");

Unfortunately there is no way to set the colors of the lines that connect the high- and low-values.

But, unless you have messed with other Custom attributes and if the x-values are meaningful, you can easily draw those lines, and by drawing the top and the bottom part separately you can also use different colors.

Here is an example:

private void chart6_PostPaint(object sender, ChartPaintEventArgs e)
{
    ChartArea ca = chart6.ChartAreas[0];
    Series s = chart6.Series[0];
    Pen hiPen = Pens.Green;
    Pen loPen = Pens.Red;

    if (e.ChartElement == s)
    foreach (DataPoint dp in s.Points)
    {
        float x       = (float)ca.AxisX.ValueToPixelPosition(dp.XValue);
        float y_hi    = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[0]);
        float y_low   = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[1]);
        float y_open  = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[2]);
        float y_close = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[3]);

        e.ChartGraphics.Graphics.DrawLine(hiPen, x, y_low, x, Math.Min(y_close, y_open));
        e.ChartGraphics.Graphics.DrawLine(loPen, x, y_hi,  x, Math.Max(y_close, y_open));
    }
}

To set Colors for the 2nd Chart's points you need to loop over the points as there Colors cannot be set in binding.

The code is simple:

void SetColors(Series candles, Series columns)
{
    for (int i = 0; i < candles.Points.Count; i++)
    {
        DataPoint dp = candles.Points[i];
        columns.Points[i].Color =
            dp.YValues[2] > dp.YValues[3] ? Color.Red :  Color.Green;
    }
}

Call it after binding!

Result:

Note that to avoid seeing the original lines shine through we set the BorderWidth to 0:

someSeries.BorderWidth = 0;


来源:https://stackoverflow.com/questions/46316572/ms-chart-candlestick-how-to-set-tail-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!