这是使用PostPaint 事件的变体:
private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
if (chart1.Series[0].Points.Count <= 0) return;
Graphics g = e.ChartGraphics.Graphics;
ChartArea ca = chart1.ChartAreas[0];
Rectangle rip = Rectangle.Round(InnerPlotPositionClientRectangle(chart1, ca));
StringFormat fmt = new StringFormat()
{ Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
int cols = chart1.Series[0].Points.Count;
int sCount = chart1.Series.Count;
int w = rip.Width / (cols + 1); // there is ca. 1/2 column gap to the sides
for (int i = 0; i < cols; i++)
{
List<string> s = (List<string>)(chart1.Series[0].Points[i].Tag);
for (int j = 0; j < s.Count; j++)
{
// change magic numbers with your font!
Rectangle r = new Rectangle(rip.Left + i * w + w / 2,
rip.Bottom + 5 + 25 * j, w, 25);
// 1st row: header, 2nd row sum, rest moved up by and reversed
using (SolidBrush brush = new SolidBrush(j == 0 ? Color.Transparent
: j == 1 ? Color.Gray : chart1.Series[sCount + 1 - j].Color))
g.FillRectangle(brush, r);
g.DrawRectangle(Pens.White, r);
g.DrawString(s[j], ca.AxisX.LabelStyle.Font, Brushes.White, r, fmt);
}
}
}
它使用相同的例程来收集标签字符串,但不是设置AxisLabels,而是将它们添加到DataPoints的Tags:
string l = months[i] + "\n" + sum + label;
chart.Series[0].Points[i].Tag = l.Split('\n').ToList();
图表样式几乎占据了代码的最大部分:
chart.BackColor = Color.DarkSlateGray;
ChartArea ca = chart.ChartAreas[0];
chart.Legends[0].Alignment = StringAlignment.Center;
chart.Legends[0].Docking = Docking.Top;
chart.Legends[0].BackColor = chart.BackColor;
chart.Legends[0].ForeColor = Color.White;
Legend L = chart.Legends[0];
L.CustomItems.Add(Color.Silver, "Sum");
ca.BackColor = Color.LightSteelBlue;
ca.Position = new ElementPosition(2, 8, 93, 70); // make room
ca.Area3DStyle.Enable3D = true;
ca.Area3DStyle.PointDepth = 25;
ca.Area3DStyle.WallWidth = 0;
ca.AxisX.MajorGrid.Enabled = false;
ca.AxisY.MajorGrid.LineColor = Color.White;
ca.AxisY.LineColor = Color.White;
ca.AxisY.LabelStyle.ForeColor = Color.White;
ca.AxisY.MajorTickMark.LineColor = Color.White;
ca.AxisX.LabelStyle.Enabled = false;
ca.AxisX.LineColor = Color.White;
ca.AxisX.MajorTickMark.Enabled = false;
使用Colors 创建Series 后,您需要应用它们,以便可以在代码中访问它们:
chart1.ApplyPaletteColors();
Series 的漂亮圆角列是由 CustomProperty 创建的
s.SetCustomProperty("DrawingStyle", "Cylinder");
更多细节:
chart.Series[1].Color = Color.Crimson;
chart.Series[0].LegendText = "Hobbits";
..
更新:您需要在我的其他一些帖子中包含两个函数InnerPlotPositionClientRectangle 和ChartAreaClientRectangle,例如here 或here!
要制作这个work in ASP,您需要在PageLoad 中连接事件:
chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(chart1_PostPaint);