Windows表格图表标签修改
Windows form Chart label modification
正在创建一个 windows 表单应用程序,它使用 chart.I 想要修改图表
- 如何编辑图表区背景颜色(屏幕截图中为白色)
- 如何给每个条应用不同的颜色。
- 如何格式化Y轴标签(需要将文件扩展名左对齐,整数值右对齐)
这是我当前的屏幕截图
需要像这样更新我的屏幕
是否可以像这样对齐标签
这是一个例子:
要设置 Chart
的样式,您可以使用:
// prepare:
chart1.Series.Clear();
Series S1 = chart1.Series.Add("S1");
ChartArea CA = chart1.ChartAreas[0];
// style type, font, color, axes
S1.ChartType = SeriesChartType.Bar;
CA.BackColor = Color.AliceBlue;
chart1.BackColor = CA.BackColor;
Font f = new Font("Consolas", 10f);
CA.AxisX.LabelStyle.Font = f;
S1.BackGradientStyle = GradientStyle.TopBottom;
CA.AxisX.MajorGrid.Enabled = false;
CA.AxisX.MajorTickMark.Enabled = false;
CA.AxisX.LineColor = Color.Transparent;
CA.AxisY.Enabled = AxisEnabled.False;
CA.AxisY.MajorGrid.Enabled = false;
CA.AxisY.MajorTickMark.Enabled = false;
要为个人 DataPoints
着色,您必须设置他们的 Color
:
S1.Points[0].Color = Color.YellowGreen;
S1.Points[1].Color = Color.YellowGreen;
要创建 formatted 标签,您可以创建字符串并在添加点时将它们用作(伪)X-Values:
string label = string.Format("{0,-11}{1, 7:0.0}%{2,8:##0.0}GB ",
t.Item1, t.Item2 * 100d / total, t.Item2) + "\u2001\u2001";
int idx = S1.Points.AddXY(label, t.Item2);
这里我用一个Tuple<string, int>
来保存我的数据。您将需要使它适应您的数据源。请注意我是如何计算总百分比的。
这是我用于示例数据的完整代码:
List<Tuple<string, double>> data = new List<Tuple<string, double>>()
{
new Tuple<string, double>( "0-1 months", 4 ),
new Tuple<string, double>( "2-3 months", 14 ),
new Tuple<string, double>( "4-11 months", 44 ),
new Tuple<string, double>( "1-2 years", 23 ),
new Tuple<string, double>( "3-5 years", 3 ),
new Tuple<string, double>( "> 5 years", 100 ),
};
double total = data.Sum(x => x.Item2);
foreach (Tuple<string, double> t in data)
{
string label = string.Format("{0,-11}{1, 7:0.0}%{2,8:##0.0}GB",
t.Item1, t.Item2 * 100d / total, t.Item2) + "\u2001\u2001";
int i = S1.Points.AddXY(label, t.Item2);
S1.Points[i].Font = f;
}
备注:
- 要获得内部字符串对齐,您需要使用 等宽 字体,如
Consolas
。
- 另请注意,您不能在一个标签内使用不同的字体或样式。
- 我添加两个 m-space characters 来创建标签和绘图区域之间的距离。 (普通空格不会显示!)。
更新:
第二个图表图像显示 SeriesChartType.Column
。要插入新行,您需要做的就是插入一个 \n
字符并确保有足够的空间。
棘手的部分是用不同颜色的一行。标签无法做到这一点。
相反,您需要为每个数据点添加两个 CustomLabels
:第一个显示例如黑线。第二个可以有不同的 ForeColor
并且需要在开头有与第一个有行一样多的 \n
。
注意 CustomLabels
坐在两个位置的中间。所以我现在添加了带有实数的 DataPoints
,从 0 开始,作为它们的 X-Values 然后将 CustomLabels
定位在..
中间
chart1.Series.Clear();
Series S1 = chart1.Series.Add("S1");
S1.ChartType = SeriesChartType.Column;
ChartArea CA = chart1.ChartAreas[0];
chart1.Legends.Clear();
CA.BackColor = Color.AliceBlue;
chart1.BackColor = Color.AliceBlue;
Font f = new Font("Consolas", 9f);
CA.AxisX.LabelStyle.Font = f;
S1.BackGradientStyle = GradientStyle.LeftRight;
CA.AxisX.MajorGrid.Enabled = false;
CA.AxisX.MajorTickMark.Enabled = false;
CA.AxisX.LineColor = Color.Transparent;
CA.AxisY.Enabled = AxisEnabled.False;
CA.AxisY.MajorGrid.Enabled = false;
CA.AxisY.MajorTickMark.Enabled = false;
CA.Position.X = 0f;
List<Tuple<string, double>> data = new List<Tuple<string, double>>()
{
new Tuple<string, double>( "0-1 months", 4 ),
new Tuple<string, double>( "2-3 months", 14 ),
new Tuple<string, double>( "4-11 months", 44 ),
new Tuple<string, double>( "1-2 years", 23 ),
new Tuple<string, double>( "3-5 years", 3 ),
new Tuple<string, double>( "> 5 years", 100 ),
};
double total = data.Sum(x => x.Item2);
foreach (Tuple<string, double> t in data)
{
string label1 = string.Format("{0}\n{1:0.0}%", t.Item1, t.Item2 * 100d / total);
string label2 = string.Format("\n\n{0:##0.0}GB", t.Item2);
int i = S1.Points.AddXY(S1.Points.Count, t.Item2);
S1.Points[i].Font = f;
DataPoint dp = S1.Points[i];
int v = (int)dp.YValues[0];
CustomLabel cl = new CustomLabel();
cl.Text = label1;
cl.FromPosition = i - 0.5f;
cl.ToPosition = i + 0.5f;
CustomLabel cl2 = new CustomLabel();
cl2.Text = label2;
cl2.FromPosition = i -0.5f;
cl2.ToPosition = i + 0.5f;
cl2.ForeColor = Color.Green;
CA.AxisX.CustomLabels.Add(cl);
CA.AxisX.CustomLabels.Add(cl2);
}
S1.Points[0].Color = Color.YellowGreen;
S1.Points[1].Color = Color.YellowGreen;
正在创建一个 windows 表单应用程序,它使用 chart.I 想要修改图表
- 如何编辑图表区背景颜色(屏幕截图中为白色)
- 如何给每个条应用不同的颜色。
- 如何格式化Y轴标签(需要将文件扩展名左对齐,整数值右对齐)
这是我当前的屏幕截图
需要像这样更新我的屏幕
是否可以像这样对齐标签
这是一个例子:
要设置 Chart
的样式,您可以使用:
// prepare:
chart1.Series.Clear();
Series S1 = chart1.Series.Add("S1");
ChartArea CA = chart1.ChartAreas[0];
// style type, font, color, axes
S1.ChartType = SeriesChartType.Bar;
CA.BackColor = Color.AliceBlue;
chart1.BackColor = CA.BackColor;
Font f = new Font("Consolas", 10f);
CA.AxisX.LabelStyle.Font = f;
S1.BackGradientStyle = GradientStyle.TopBottom;
CA.AxisX.MajorGrid.Enabled = false;
CA.AxisX.MajorTickMark.Enabled = false;
CA.AxisX.LineColor = Color.Transparent;
CA.AxisY.Enabled = AxisEnabled.False;
CA.AxisY.MajorGrid.Enabled = false;
CA.AxisY.MajorTickMark.Enabled = false;
要为个人 DataPoints
着色,您必须设置他们的 Color
:
S1.Points[0].Color = Color.YellowGreen;
S1.Points[1].Color = Color.YellowGreen;
要创建 formatted 标签,您可以创建字符串并在添加点时将它们用作(伪)X-Values:
string label = string.Format("{0,-11}{1, 7:0.0}%{2,8:##0.0}GB ",
t.Item1, t.Item2 * 100d / total, t.Item2) + "\u2001\u2001";
int idx = S1.Points.AddXY(label, t.Item2);
这里我用一个Tuple<string, int>
来保存我的数据。您将需要使它适应您的数据源。请注意我是如何计算总百分比的。
这是我用于示例数据的完整代码:
List<Tuple<string, double>> data = new List<Tuple<string, double>>()
{
new Tuple<string, double>( "0-1 months", 4 ),
new Tuple<string, double>( "2-3 months", 14 ),
new Tuple<string, double>( "4-11 months", 44 ),
new Tuple<string, double>( "1-2 years", 23 ),
new Tuple<string, double>( "3-5 years", 3 ),
new Tuple<string, double>( "> 5 years", 100 ),
};
double total = data.Sum(x => x.Item2);
foreach (Tuple<string, double> t in data)
{
string label = string.Format("{0,-11}{1, 7:0.0}%{2,8:##0.0}GB",
t.Item1, t.Item2 * 100d / total, t.Item2) + "\u2001\u2001";
int i = S1.Points.AddXY(label, t.Item2);
S1.Points[i].Font = f;
}
备注:
- 要获得内部字符串对齐,您需要使用 等宽 字体,如
Consolas
。 - 另请注意,您不能在一个标签内使用不同的字体或样式。
- 我添加两个 m-space characters 来创建标签和绘图区域之间的距离。 (普通空格不会显示!)。
更新:
第二个图表图像显示 SeriesChartType.Column
。要插入新行,您需要做的就是插入一个 \n
字符并确保有足够的空间。
棘手的部分是用不同颜色的一行。标签无法做到这一点。
相反,您需要为每个数据点添加两个 CustomLabels
:第一个显示例如黑线。第二个可以有不同的 ForeColor
并且需要在开头有与第一个有行一样多的 \n
。
注意 CustomLabels
坐在两个位置的中间。所以我现在添加了带有实数的 DataPoints
,从 0 开始,作为它们的 X-Values 然后将 CustomLabels
定位在..
chart1.Series.Clear();
Series S1 = chart1.Series.Add("S1");
S1.ChartType = SeriesChartType.Column;
ChartArea CA = chart1.ChartAreas[0];
chart1.Legends.Clear();
CA.BackColor = Color.AliceBlue;
chart1.BackColor = Color.AliceBlue;
Font f = new Font("Consolas", 9f);
CA.AxisX.LabelStyle.Font = f;
S1.BackGradientStyle = GradientStyle.LeftRight;
CA.AxisX.MajorGrid.Enabled = false;
CA.AxisX.MajorTickMark.Enabled = false;
CA.AxisX.LineColor = Color.Transparent;
CA.AxisY.Enabled = AxisEnabled.False;
CA.AxisY.MajorGrid.Enabled = false;
CA.AxisY.MajorTickMark.Enabled = false;
CA.Position.X = 0f;
List<Tuple<string, double>> data = new List<Tuple<string, double>>()
{
new Tuple<string, double>( "0-1 months", 4 ),
new Tuple<string, double>( "2-3 months", 14 ),
new Tuple<string, double>( "4-11 months", 44 ),
new Tuple<string, double>( "1-2 years", 23 ),
new Tuple<string, double>( "3-5 years", 3 ),
new Tuple<string, double>( "> 5 years", 100 ),
};
double total = data.Sum(x => x.Item2);
foreach (Tuple<string, double> t in data)
{
string label1 = string.Format("{0}\n{1:0.0}%", t.Item1, t.Item2 * 100d / total);
string label2 = string.Format("\n\n{0:##0.0}GB", t.Item2);
int i = S1.Points.AddXY(S1.Points.Count, t.Item2);
S1.Points[i].Font = f;
DataPoint dp = S1.Points[i];
int v = (int)dp.YValues[0];
CustomLabel cl = new CustomLabel();
cl.Text = label1;
cl.FromPosition = i - 0.5f;
cl.ToPosition = i + 0.5f;
CustomLabel cl2 = new CustomLabel();
cl2.Text = label2;
cl2.FromPosition = i -0.5f;
cl2.ToPosition = i + 0.5f;
cl2.ForeColor = Color.Green;
CA.AxisX.CustomLabels.Add(cl);
CA.AxisX.CustomLabels.Add(cl2);
}
S1.Points[0].Color = Color.YellowGreen;
S1.Points[1].Color = Color.YellowGreen;