x轴上的自定义标签 [英] custom label on x-axis

查看:304
本文介绍了x轴上的自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图表预测未来30年的价值。第一个值必须显示为年1,然后是5年,10年...直到30.但内部第一年是0,并且被关闭:

My chart predicts a value for the next 30 years. The first value must be displayed as year 1. Then year 5, 10... until 30. But internally the first year is 0 and is left off:

我尝试添加自定义标签,但它只打破其他标签:

I tried adding a custom label, but it only breaks the other labels:

如果我将它添加到AxisX2而不是AxisX,它什么也不做。
下面是代码,使图表和添加行:

If I add it to AxisX2 instead of AxisX it does nothing. Here is the code to make the chart and add the lines:

public static Chart MakeChart(string title)
{
    var chart = new Chart();

    var area = new ChartArea("GrafiekGebied");

    foreach (var axis in area.Axes)
    {
        axis.TitleForeColor = defaultColor;
        axis.LineColor = defaultColor;
        axis.InterlacedColor = defaultColor;
        axis.LabelStyle.Font = letterType;
        axis.LabelAutoFitMinFontSize = (int)letterType.Size;
        axis.LabelAutoFitMaxFontSize = (int)letterType.Size;
        axis.MajorGrid.LineColor = defaultColor;
        axis.MajorTickMark.Enabled = false;
        axis.MinorGrid.LineColor = defaultColor;
        axis.MinorTickMark.LineColor = defaultColor;
    }

    CustomLabel firstXlabel = new CustomLabel();
    firstXlabel.FromPosition = 0;
    firstXlabel.ToPosition = 0;
    firstXlabel.RowIndex = 0; // Also tried 1
    firstXlabel.Text = "1jr";

    area.AxisY.LineWidth = 0;
    area.AxisY.LabelStyle.Format = "€{#,##}";
    area.AxisX.TextOrientation = TextOrientation.Horizontal;
    area.AxisX.CustomLabels.Add(firstXlabel); // Adding it to AxisX2 does nothing
    area.AxisX.IsMarginVisible = true;
    area.AxisX.MajorGrid.Enabled = false;
    area.AxisX.IntervalOffset = 1;
    area.AxisX.LabelStyle.Format = "{#}jr";
    area.AxisX.MajorTickMark.Enabled = true;
    area.AxisX2.LineWidth = 1;
    area.AxisX2.LineColor = Color.Green;

    var legend = new Legend();
    legend.LegendStyle = LegendStyle.Row;
    legend.Docking = Docking.Bottom;
    legend.DockedToChartArea = area.Name;
    legend.Font = lettering;
    legend.IsDockedInsideChartArea = false;

    chart.ForeColor = defaultColor;
    chart.Font.Name = lettering.FontFamily.Name;
    chart.Font.Size = new System.Web.UI.WebControls.FontUnit(lettering.Size);
    chart.Width = 280;
    chart.Height = 180;
    chart.Legends.Add(legend);
    chart.ChartAreas.Add(area);
    chart.BorderlineColor = defaultColor;
    chart.BorderlineWidth = 1;
    chart.BorderlineDashStyle = ChartDashStyle.Solid;
    chart.Titles.Add(title);

    return chart;
}

public static void AddData(Chart chart, ChartInput input)
{
    var line = new Series(input.Subtitle);
    line.ChartArea = chart.ChartAreas[0].Name;
    line.ChartType = SeriesChartType.Spline;
    line.Color = input.Color;
    line.BorderWidth = 3;
    line.MarkerStyle = MarkerStyle.Circle;
    line.MarkerSize = 7;
    line.MarkerStep = 5;

    for (int i = 0; i < input.Waarden.Length; i++)
    {
        line.Points.AddXY(i, input.Values[i]);
    }

    chart.Series.Add(line);
}

创建图形后,使用Aspose将其插入到word文档中,

After making the graph it's inserted in a word document using Aspose, but that should not matter for how the chart is made.

推荐答案

这可以通过为每个x值添加一个自定义标签,年。

This is possible by adding a custom label for every x value, so for each year.

Dim series1 As New Series("Series1")
series1.ChartType = SeriesChartType.Column

' Adding some points
series1.Points.AddXY(1, 1)
series1.Points.AddXY(2, 1)
series1.Points.AddXY(3, 1)

Chart1.Series.Add(series1)

' I manually set the maxium of the X axis
Chart1.ChartAreas("ChartArea1").AxisX.Interval = 1
Chart1.ChartAreas("ChartArea1").AxisX.Maximum = 4

Chart1.ChartAreas("ChartArea1").AxisX.CustomLabels.Add(0.5, 1.5, "yr1")
Chart1.ChartAreas("ChartArea1").AxisX.CustomLabels.Add(1.5, 2.5, "yr2")
Chart1.ChartAreas("ChartArea1").AxisX.CustomLabels.Add(2.5, 3.5, "yr3")

如果点的x值为'1'应该将customlabel从'0.5'添加到'1.5'。这种方式很好地集中。

If a point has a x-value of '1', then you should add the customlabel from '0.5' to '1.5'. That way it centers nicely.

这篇关于x轴上的自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆