In a 烛台图 https://msdn.microsoft.com/en-us/library/dd456671.aspx有CustomProperties
根据趋势自动设置框的颜色:
someSeries.SetCustomProperty("PriceUpColor", "Green");
someSeries.SetCustomProperty("PriceDownColor", "Red");
不幸的是,无法设置连接高值和低值的线条的颜色。
但是,除非您弄乱了其他自定义属性并且 x 值有意义,否则您可以轻松绘制这些线条,并且通过分别绘制顶部和底部部分,您还可以使用不同的颜色。
这是一个例子:
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));
}
}
要为第二个图表的点设置颜色,您需要循环遍历这些点,因为那里的颜色cannot https://stackoverflow.com/questions/33027058/binding-list-of-lists-to-chart-control/33082008?s=6%7C0.0956#33082008被设置为绑定。
代码很简单:
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;
}
}
绑定后调用即可!
Result:
请注意,为了避免看到原始线条透出,我们设置了BorderWidth
to 0:
someSeries.BorderWidth = 0;