【问题标题】:How to change color of axis scale In TeeChart如何在 TeeChart 中更改轴刻度的颜色
【发布时间】:2013-03-07 21:21:58
【问题描述】:

我正在使用来自应用程序的 C# 窗口。我正在使用 .net v3 的 TeeChart 来绘制图表。我可以为每个 Y 轴创建不同颜色的多个 Y 轴,如下图所示。 现在我可以用不同的颜色显示轴,但我也想为其轴的比例分配相同的轴颜色。请帮助我需要使用什么属性。

另一个问题是,如果我有多个轴,那么在图表上为每个轴创建单独的轴会占用很多空间。我想水平分配轴的比例,而不是像我现在得到的那样。请任何人帮助我请我需要使用哪些属性。 我想表示比例和轴,如下图所示。

提前致谢。

【问题讨论】:

    标签: c# winforms teechart


    【解决方案1】:

    我编写了一个简单的代码,在其中为我绘制的每个自定义轴设置了相同的比例,并自动将所有轴放置在正确的位置。我认为您可以使用与 next 类似的代码来尝试实现您想要的效果:

    public Form1()
            {
                InitializeComponent();
                InitializeChart();
            }
    
            private DataSet GetData()
            {
                DataSet TeeDataSet = new DataSet();
                DateTime dt = DateTime.Today;
                DataTable TeeDataTable = new DataTable("DataTable1");
                DataColumn xval = new DataColumn("DateTime", typeof(DateTime));
                DataColumn yval = new DataColumn("SystemName", typeof(double));
    
                TeeDataTable.Columns.Add(xval);
                TeeDataTable.Columns.Add(yval);
                Random rnd = new Random();
                for (int i = 0; i < 10; i++)
                {
                    DataRow newRow = TeeDataTable.NewRow();
                    newRow[xval] = dt;
                    newRow[yval] = rnd.Next(100);
                    TeeDataTable.Rows.Add(newRow);
                    dt = dt.AddMonths(1);
                }
                TeeDataSet.Tables.Add(TeeDataTable);
                return TeeDataSet;
            }
            private void InitializeChart()
            {
                tChart1.Aspect.View3D = false;
                tChart1.Header.Visible = false;
                tChart1.Legend.Alignment = LegendAlignments.Bottom;
                tChart1.Legend.CheckBoxes = true;
                for (int i = 0; i < 5; i++)
                {
                    new Steema.TeeChart.Styles.Line(tChart1.Chart);
                    tChart1[i].Title = "SystemName";
                    tChart1[i].DataSource = GetData();//Add values using DataSource
                    tChart1[i].XValues.DataMember = "DateTime";
                    tChart1[i].XValues.DateTime = true;
                    tChart1[i].XValues.Order = Steema.TeeChart.Styles.ValueListOrder.Ascending;
                    tChart1[i].YValues.DataMember = "SystemName";
                    tChart1.Axes.Custom.Add(new Steema.TeeChart.Axis(tChart1.Chart));
                    tChart1[i].CustomVertAxis = tChart1.Axes.Custom[i];
                    tChart1.Axes.Custom[i].AxisPen.Color = tChart1[i].Color;
                    tChart1.Axes.Custom[i].Grid.Visible = false;
    
                    tChart1.Axes.Custom[i].PositionUnits = PositionUnits.Pixels;
                }
    
                tChart1.Panel.MarginUnits = PanelMarginUnits.Pixels;
                tChart1.Panel.MarginTop = 20;
                tChart1.Draw();
                PlaceAxes(0, 0, 0, 0, 0);
                tChart1.AfterDraw += new PaintChartEventHandler(tChart1_AfterDraw);
                tChart1.ClickLegend += new MouseEventHandler(tChart1_ClickLegend);
                tChart1.Draw();
            }
    
            void tChart1_ClickLegend(object sender, MouseEventArgs e)
            {
                tChart1.Draw();
            }
    
            void tChart1_AfterDraw(object sender, Graphics3D g)
            {
                PlaceAxes(0, 0, 0, 0, 0);
            }
    
            private void PlaceAxes(int nSeries, int NextXLeft, int NextXRight, int MargLeft, int MargRight)
            {
                const int extraPos = 12;
                const int extraMargin = 60;
                //Variable 
                int MaxLabelsWidth;
                int lenghtTicks;
                int extraSpaceBetweenTitleAndLabels;
                foreach (Steema.TeeChart.Styles.Line s in tChart1.Series)
                {
                    if (s.Active)
                    {
                        s.CustomVertAxis.Visible = true;
                        s.CustomVertAxis.SetMinMax(tChart1[0].YValues.Minimum, tChart1[0].YValues.Maximum);
                        MaxLabelsWidth = s.CustomVertAxis.MaxLabelsWidth();
                        lenghtTicks = s.CustomVertAxis.Ticks.Length;
                        extraSpaceBetweenTitleAndLabels = (s.CustomVertAxis.Title.Width);//- tChart1.Axes.Custom[nSeries].MaxLabelsWidth());
                        if (s.CustomVertAxis.Title.Visible)
                        {
                            s.CustomVertAxis.RelativePosition = NextXLeft;
                            NextXLeft = NextXLeft - (MaxLabelsWidth + lenghtTicks + extraSpaceBetweenTitleAndLabels + extraPos);
                            MargLeft = MargLeft + extraMargin;
                        }
    
                        else
                        {
                            s.CustomVertAxis.RelativePosition = NextXLeft;
                            NextXLeft = NextXLeft - (MaxLabelsWidth + lenghtTicks + extraPos);
                            MargLeft = MargLeft + extraMargin;
                        }
    
                        tChart1.Panel.MarginLeft = MargLeft;
                        tChart1.Panel.MarginRight = MargRight;
    
                    }
                    else
                    {
                        s.CustomVertAxis.Visible = false;
                    }
                }
            }
    

    您能告诉我们之前的代码是否适用于您吗?如果您有任何问题,请告诉我。

    希望对你有帮助。

    谢谢,

    【讨论】:

    • 感谢您的重播。我没有使用数据库,我直接从传感器获取数据并更新图表。如果我连接了三个传感器,那么它将为这三个指定颜色的轴创建轴。我没有使用您的代码来绘制轴。我发布的上述代码运行良好。我为图表的初始化添加了一点代码,你可以看到。如果您需要更多信息,请询问我。
    • 感谢您的信息。您对我所做的修改对我没有帮助,但我修改了我的代码,因为系列从 DataSet 获取数据并且它对我来说很好。我认为您可以使用我在代码中添加的一些条件、方法或属性并将其应用到您的代码中,以尝试实现它在您的最终工作。如果我建议的代码对您没有帮助,如果您能给我们发送一个出现问题的简单代码示例,我们将不胜感激,因为我们可以在这里准确地重现您的问题并尝试为您找到一个好的解决方案。谢谢,
    • 你能知道我需要使用哪个属性来更改轴刻度字母的颜色吗?
    • 您好 reddy,如果您想修改标签的颜色,您需要更改标签的字体颜色属性,因为字母与轴的颜色相同。您可以执行以下代码:axis.Labels.Font.Color = axis].AxisPen.Color; axis.Ticks.Color = 轴.AxisPen.Color;轴.MinorTicks.Color = 轴.AxisPen.Color;您能告诉我们之前的代码行是否适用于您吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多