C#中的条形图 [英] Bar Graphs in c#

查看:82
本文介绍了C#中的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中实现条形图.我有两个文本框,我在其中获取数据,我想为它们绘制一个比较条形图.例如,我有一个原始的cpu(ocpu)和更改的cpu(ccpu)数据,我想为其绘制一个比较图.我可以做到,如图所示.蓝色是原始值,橙色是更改后的值.但是,我在X轴上得到0,1,2,这不是我所需要的.我也不需要那两个酒吧之间的线.我还附上了我正在使用的代码段.我在图中有两个系列,分别称为原始"和修改".

  chartcpu.Series ["Original"].Points.AddY(ocpu.Text.ToString());chartcpu.Series ["Modified"].Points.AddY(ccpu.Text.ToString());chartcpu.ChartAreas [0] .AxisX.Title ="CPU"; 

如果您想实现其他目标,则可以张贴应该如何"草图(用油漆或其他任何东西).

德国的问候

更新:

  1. 我的数据来自文本框,是否不能将其转换为字符串进行显示?这是上面提到的评论.我需要将什么格式转换为?

字符串只是文本,图表需要浮点数,如 System.Windows.Forms.DataVisualization.Charting.DataPoint 的YValues属性所示:

public double [] YValues {get;放;}

Points.AddXY 已经为您完成了转换,因为它还可以处理 DateTime DBNull 等.如果需要,可以使用 Double.TryParse 自己处理转换.

  1. "AxisY.MajorGrid.Enabled = false;"的含义是什么.我认为这是我的代码中缺少的.

是的,主要和次要网格属性控制着从指定轴开始的线条的外观.通过禁用它们,您只需关闭它们即可.

  1. 如何在此处更改条形的颜色和宽度

Series和DataPoint都具有颜色属性,因此您可以说:

  chartcpu.Series ["Original"].Color = Color.Magenta;chartcpu.Series ["Modified"].Color = Color.Lime; 

或者您喜欢的任何颜色:)

大小是这样变化的:

  chartcpu.Series ["Original"].SetCustomProperty("PointWidth","2");chartcpu.Series ["Modified"].SetCustomProperty("PointWidth","2"); 

如果我的数据为空,我不想显示任何条形.现在显示0.

当我未指定任何数据时,我的图表为空.但是,只有在您拥有有效数据的情况下,您才可以尝试添加一个点:

 //例如当ocpu文本更改时chartcpu.Series [原始"] .Points.Clear();yValue的倍数= 0;如果(!String.IsNullOrEmpty(ocpu.Text)&& Double.TryParse(ocpu.Text,出yValue)){chartcpu.Series [原始"] .Points.AddXY("CPU",yValue);} 

更新:

如何更改以K格式显示在左侧网格中的值,例如-如果它是1200万,我希望将其显示为12000K/12M.另外,如何格式化带有分隔符的栏上方显示的值为1000,例如,如果值为112887296则为112,887,296或112,887K/113M(近似值)

简单方法:将此添加到您的初始化逻辑中:

 //字符串格式=#,0.#0";//默认//字符串格式=#,0,.#0K";//一千字符串格式=#,0,..#0M";//百万等cpuchart.ChartAreas [0] .AxisY.LabelStyle.Format = format;cpuchart.Series.ToList().ForEach(series => series.LabelFormat = format); 

msdn上的自定义数字格式: https://msdn.microsoft.com/zh-US/library/0c899ak8(v = vs.110).aspx

动态方法:为您的后缀添加一个字典作为静态变量:

 私有静态只读ReadOnlyDictionary< int,字符串>_numberSuffix =新的ReadOnlyDictionary< int,字符串>(new Dictionary< int,字符串> {{0,String.Empty},//默认{1,"K"},//千{2,"M"},//百万{3,"B"},//十亿{4,"T"},//万亿}); 

以及用于设置图表格式的方法:

  private void FormatChart(图表图表){//根据y轴上的最大值计算格式chart.ChartAreas [0] .RecalculateAxesScale();//确保计算出y轴的最大值int digitGroupCount =(int)Math.Log10(Math.Abs​​(chart.ChartAreas [0] .AxisY.Maximum))/3;字符串后缀= _numberSuffix.ContainsKey(digitGroupCount)吗?_numberSuffix [digitGroupCount]:"nA";字符串格式= String.Format(#,0 {0}.#0 {1}",新字符串(',',digitGroupCount),后缀);chart.ChartAreas [0] .AxisY.LabelStyle.Format = format;chart.Series.ToList().ForEach(series => series.LabelFormat = format);} 

现在,您可以在要设置图表格式时(例如,更改值时)调用此方法

I am trying to implement a bar graph in C#. I have two textboxes in which i get data and i want to plot a comparison bar graph for them. For example, i have a original cpu(ocpu) and changed cpu (ccpu) data coming and i want to plot a comparison graph for them. I am able to do so, as in the picture graph.The blue is original value and orange is changed value. But, i am getting 0,1,2 in the X axis which i don't require. I also dont require the line between those two bars. I have attached the code snippet also that i am using. I have two series called "Original" and "modified" in the graph.

chartcpu.Series["Original"].Points.AddY(ocpu.Text.ToString());
chartcpu.Series["Modified"].Points.AddY(ccpu.Text.ToString());
chartcpu.ChartAreas[0].AxisX.Title = "CPU";

data graph

Please let me know what is a solution to this ?

--Samir Singh

解决方案

I am not completly sure if i unterstood you corretly but with this code:

        var original = chart1.Series.Add("Original");
        var modified = chart1.Series.Add("Modified");
        chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
        chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = false;
        chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
        chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = false;

        original.Points.AddXY("CPU", 7.6);
        modified.Points.AddXY("CPU", 1.6);

you get this result:

You may post a "how it should be" sketch (with paint or whatever) if you want to achieve something different.

Greetings from germany

UPDATE:

  1. I have my data coming from a textbox, is it not OK to convert it to string for display. This is a comment mentioned above. What form do i need to convert it to ?

string is just text and the chart expects floating-point numbers as seen on the YValues Property of the System.Windows.Forms.DataVisualization.Charting.DataPoint:

public double[] YValues { get; set; }

Points.AddXY already does the conversion for you as its also handles DateTime or DBNull etc. If you want to you can handle the conversion yourself by using Double.TryParse.

  1. what is the meaning of "AxisY.MajorGrid.Enabled = false;". I think this was missing in my code.

Well yes, the major and minor grid properties control the appearance of the lines from the specified axis. By disabling them you simply turn them off.

  1. How do i change the colour and width of the bars here

Series and DataPoint both have the Color-Property so you can say:

chartcpu.Series["Original"].Color = Color.Magenta;
chartcpu.Series["Modified"].Color = Color.Lime;

Or whatever color you prefer :)

The size is change like this:

chartcpu.Series["Original"].SetCustomProperty("PointWidth", "2");
chartcpu.Series["Modified"].SetCustomProperty("PointWidth", "2");

If my data is empty, i want to display no bar. Now is shows 0.

When i do not specify any data my chart is empty. But you can try to only add a point if you have valid data:

// e.g. when ocpu text change
chartcpu.Series["Original"].Points.Clear();
double yValue = 0;
if (!String.IsNullOrEmpty(ocpu.Text) && Double.TryParse(ocpu.Text, out yValue))
{
    chartcpu.Series["Original"].Points.AddXY("CPU", yValue);
}

UPDATE:

how to change the value that is shown on the Left Grids in K format, for e.g - if it is 12000000, I want it to be displayed as 12000K/12M. Also, how do I format the value shown above the bar having a separator for 1000 e.g if the value is 112887296 as 112,887,296 or 112,887K/113M(approximated )

Simple approach: Add this to your initialization logic:

//string format = "#,0.#0"; // default
//string format = "#,0,.#0K"; // thousand
string format = "#,0,,.#0M"; // million, etc.
cpuchart.ChartAreas[0].AxisY.LabelStyle.Format = format;
cpuchart.Series.ToList().ForEach(series => series.LabelFormat = format);

Custom Numeric Format on msdn: https://msdn.microsoft.com/en-US/library/0c899ak8(v=vs.110).aspx

Dynamic approach: Add a Dictionary for your suffixes as a static variable:

private static readonly ReadOnlyDictionary<int, string> _numberSuffix = new ReadOnlyDictionary<int, string>(new Dictionary<int, string> {
        { 0, String.Empty }, // default
        { 1, "K" }, // thousand
        { 2, "M" }, // million
        { 3, "B" }, // billion
        { 4, "T" }, // trillion
    });

and a method for formatting your chart:

private void FormatChart(Chart chart)
{
    // calculates the format based on the maximum on the y axis
    chart.ChartAreas[0].RecalculateAxesScale(); // ensure that the maximum of the y-axis is calculated
    int digitGroupCount = (int)Math.Log10(Math.Abs(chart.ChartAreas[0].AxisY.Maximum)) / 3;
    string suffix = _numberSuffix.ContainsKey(digitGroupCount) ? _numberSuffix[digitGroupCount] : "nA";
    string format = String.Format("#,0{0}.#0{1}", new string(',', digitGroupCount), suffix);
    chart.ChartAreas[0].AxisY.LabelStyle.Format = format;
    chart.Series.ToList().ForEach(series => series.LabelFormat = format);
}

Now you can call this method whenever you want to format your chart (e.g. when a value changed)

这篇关于C#中的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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