如何避免 ZedGraph 重新标记我的 YAxis(除以 1000)?

发布于 2024-11-29 08:28:06 字数 2268 浏览 0 评论 0原文

我正在创建一个 C# Visual Studio 表单应用程序,它使用 zedgraph 来绘制程序收集的数据的图表,但在绘制数据时遇到以下问题:

我的 y 轴值通常在 100,000+ 范围内,因此当 zed graph 绘制时它用 0、10、15、20、25 等内容标记 y 轴标签的值,然后在 y 轴标签上附加“(10^3)​​”标题并将相应地绘制值。我想要做的是让它用 0、10,000、15,000、20,000 等值或 0、10k、15k、20k 等值标记 y 轴,而不让它调整 y 轴标题。

我尝试设置 YAxis.Scale.MajorStep = double.Parse("10000"); ,但唯一的效果是在 y 轴上添加大量刻度线,但没有其他效果。这是我的绘制数据图表的代码:

    private void createGraph()
    {
        GraphPane myPane = zdc_graph.GraphPane;
        myPane.CurveList.Clear();
        myPane.GraphObjList.Clear();

        myPane.Title.Text = this.monitoredHost.hostName + "\nWorkState[" +
                            this.monitoredHost.currentWorkState + "]";
        myPane.XAxis.Title.Text = "";

        myPane.YAxis.Title.Text = "OPS Per Second";
        myPane.YAxis.Scale.FontSpec.FontColor = Color.Blue;
        myPane.YAxis.Title.FontSpec.FontColor = Color.Blue;
        myPane.YAxis.Scale.MaxAuto = true;

        myPane.Y2Axis.Title.Text = "Reading";
        myPane.Y2Axis.IsVisible = true;
        myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Red;
        myPane.Y2Axis.Title.FontSpec.FontColor = Color.Red;

        myPane.XAxis.Type = AxisType.Date;
        myPane.XAxis.Scale.Format = "T";
        myPane.XAxis.Scale.MajorUnit = DateUnit.Second;
        myPane.YAxis.Scale.Min = 0;
        myPane.YAxis.Scale.MajorStep = double.Parse("10000");
        myPane.Y2Axis.Scale.Min = 0;

        LineItem kpiCurve = myPane.AddCurve("OPS Per Second",
                           this.monitoredHost.graphKpiList,
                           Color.Blue,SymbolType.Circle);
        LineItem pwrCurve = myPane.AddCurve("Reading", 
                           this.monitoredHost.graphPwrList, Color.Red, 
                           SymbolType.Circle);

        kpiCurve.Line.Width = 2.0F;
        kpiCurve.Symbol.Size = 4.0F;
        kpiCurve.Symbol.Fill = new Fill(Color.White);

        pwrCurve.Line.Width = 2.0F;
        pwrCurve.Symbol.Size = 4.0F;
        pwrCurve.Symbol.Fill = new Fill(Color.White);
        pwrCurve.IsY2Axis = true;

        myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 210), -45F);

        zdc_graph.AxisChange();
        zdc_graph.Refresh();
    }

我希望这是有道理的。感谢您的帮助。

I am creating a C# visual studio forms application that uses zedgraph to chart the data that the program collects but I am running into the following issue when plotting the data:

My y-axis values are usually in the 100,000+ range so when zed graph plots the value it labels the y-axis labels with stuff like 0, 10, 15, 20, 25 and then on the y-axis label it will append "(10^3)" to the title and will plot the values accordingly. What I want to do is have it label the y-axis either with values like 0, 10,000, 15,000, 20,000 etc or 0, 10k, 15k, 20k and so on and not have it adjust the y-axis title.

I tried setting YAxis.Scale.MajorStep = double.Parse("10000"); but the only effect that has is to add a ton of more tick lines on the y-axis but no other effect. Here is my code that graphs the data:

    private void createGraph()
    {
        GraphPane myPane = zdc_graph.GraphPane;
        myPane.CurveList.Clear();
        myPane.GraphObjList.Clear();

        myPane.Title.Text = this.monitoredHost.hostName + "\nWorkState[" +
                            this.monitoredHost.currentWorkState + "]";
        myPane.XAxis.Title.Text = "";

        myPane.YAxis.Title.Text = "OPS Per Second";
        myPane.YAxis.Scale.FontSpec.FontColor = Color.Blue;
        myPane.YAxis.Title.FontSpec.FontColor = Color.Blue;
        myPane.YAxis.Scale.MaxAuto = true;

        myPane.Y2Axis.Title.Text = "Reading";
        myPane.Y2Axis.IsVisible = true;
        myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Red;
        myPane.Y2Axis.Title.FontSpec.FontColor = Color.Red;

        myPane.XAxis.Type = AxisType.Date;
        myPane.XAxis.Scale.Format = "T";
        myPane.XAxis.Scale.MajorUnit = DateUnit.Second;
        myPane.YAxis.Scale.Min = 0;
        myPane.YAxis.Scale.MajorStep = double.Parse("10000");
        myPane.Y2Axis.Scale.Min = 0;

        LineItem kpiCurve = myPane.AddCurve("OPS Per Second",
                           this.monitoredHost.graphKpiList,
                           Color.Blue,SymbolType.Circle);
        LineItem pwrCurve = myPane.AddCurve("Reading", 
                           this.monitoredHost.graphPwrList, Color.Red, 
                           SymbolType.Circle);

        kpiCurve.Line.Width = 2.0F;
        kpiCurve.Symbol.Size = 4.0F;
        kpiCurve.Symbol.Fill = new Fill(Color.White);

        pwrCurve.Line.Width = 2.0F;
        pwrCurve.Symbol.Size = 4.0F;
        pwrCurve.Symbol.Fill = new Fill(Color.White);
        pwrCurve.IsY2Axis = true;

        myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 210), -45F);

        zdc_graph.AxisChange();
        zdc_graph.Refresh();
    }

I hope this makes sense. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ZedGraph 正在尝试检测幅度并简化图表。您可以通过以下方式关闭此功能:

myPane.YAxis.Scale.MagAuto = false;

这将产生 y 轴标签,如 100000

如果您想使用分隔符逗号设置标签格式,例如 100,000

myPane.YAxis.Scale.Format = "#,#";

最后,如果您希望显示 100k,则需要订阅 ScaleFormatEvent< /code> 并返回您自己的格式,如下所示:

myPane.YAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(YAxis_ScaleFormatEvent);

string YAxis_ScaleFormatEvent(GraphPane pane, Axis axis, double val, int index)
{
    return String.Format("{0}k", val / 1000);
}

ZedGraph is attempting to detect magnitude and simplify the graph. You can turn this off with the following:

myPane.YAxis.Scale.MagAuto = false;

This will result in y-axis labels like 100000.

If you want to format the label with a separator comma like 100,000:

myPane.YAxis.Scale.Format = "#,#";

Finally, if you prefer to show 100k, you'll need to subscribe to the ScaleFormatEvent and return your own format, like this:

myPane.YAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(YAxis_ScaleFormatEvent);

string YAxis_ScaleFormatEvent(GraphPane pane, Axis axis, double val, int index)
{
    return String.Format("{0}k", val / 1000);
}
后知后觉 2024-12-06 08:28:06

我有类似的问题。因此,应用您的方法,它可以在应用程序上工作,但我也想打印 PDF 文件中的图形(使用 MigraDoc),但它确实有效。

       public Bitmap printGraphPane()
       {
        ZedGraphControl graph = new ZedGraphControl();
        GraphPane newGP = myPane.GraphPane;
        //newGP.YAxis.Scale.Mag = 0;
        //newGP.YAxis.Scale.Format = "#";
        //newGP.YAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(YAxis_ScaleFormatEvent);

        Bitmap bit = new Bitmap(newGraph.Width, newGraph.Height);
        newGraph.ClientSize = bit.Size;
        newGraph.DrawToBitmap(bit, new Rectangle(0, 0, newGraph.Width, newGraph.Height));

        return bit;
       }

I am having similar problem. So applying your method it works on the application but i also want to print out the graph in a PDF file (using MigraDoc) but it does work.

       public Bitmap printGraphPane()
       {
        ZedGraphControl graph = new ZedGraphControl();
        GraphPane newGP = myPane.GraphPane;
        //newGP.YAxis.Scale.Mag = 0;
        //newGP.YAxis.Scale.Format = "#";
        //newGP.YAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(YAxis_ScaleFormatEvent);

        Bitmap bit = new Bitmap(newGraph.Width, newGraph.Height);
        newGraph.ClientSize = bit.Size;
        newGraph.DrawToBitmap(bit, new Rectangle(0, 0, newGraph.Width, newGraph.Height));

        return bit;
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文