没有内置的方法可以做到这一点。
这对于 column 和 bar 类型来说并不容易,因为没有暴露列的大小。
另外请注意,重叠的列确实有点难以阅读,尤其是。当你也有Labels。
以下是所有者绘制柱形图的示例。它有几个简化:
所有Series 具有相同数量的点并且对齐,所有y 值都是正数,并且没有其他装饰。他们可能都被克服了,但可能需要一些额外的努力..
private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
if (!checkBox2.Checked) return;
int sMax = chart1.Series.Count;
ChartArea ca = chart1.ChartAreas[0];
Axis ax = ca.AxisX;
Axis ay = ca.AxisY;
float py0 = (float)ay.ValueToPixelPosition(ay.Minimum);
Rectangle ipr = Rectangle.Round(InnerPlotPositionClientRectangle(chart1, ca));
int pMax = chart1.Series[0].Points.Count;
float shift = (overlap * sMax) / 2f;
float deltaX = 1f * ipr.Width / (pMax+1);
float colWidth = 1f * deltaX / sMax;
for (int j = 0; j < chart1.Series.Count; j++)
for (int i = 0; i < chart1.Series[j].Points.Count; i++)
{
DataPoint dp = chart1.Series[j].Points[i];
float px = (float)ax.ValueToPixelPosition(dp.XValue);
float py = (float)ay.ValueToPixelPosition(dp.YValues[0]);
using (SolidBrush brush = new SolidBrush(chart1.Series[j].Color))
e.ChartGraphics.Graphics.FillRectangle(brush,
px + j * colWidth - deltaX / 2 - overlap * j + shift, py,
colWidth, py0 - py );
}
}
它使用了一个函数InnerPlotPositionClientRectangle,你可以找到here
结果如下:
请注意,要访问系列颜色,您需要将它们apply 发送到图表:
chart1.ApplyPaletteColors();
列宽设置如下:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
for (int j = 0; j < chart1.Series.Count; j++)
chart1.Series[j]["PointWidth"] = numericUpDown1.Value.ToString();
}
在“0”处,列消失。