![](/img/trans.png)
[英]How I can know X value of point (X, Y) in chart and I know the value of Y C#?
[英]how to add value in 'y' on same 'x' point in c# chart
我正在嘗試基於字符串數組創建柱形圖。 如果超過 1 個字符串對應於相同的字符串值,則應將數字相加並使用相同的條形來表示相似的字符串。 然而,我的這段代碼導致在不同的條形上表示(每個條形的值都是 1):
private void plot_chart(string[] DTCs)
{
foreach (string str in DTCs)
{
bool doNotaddSeries = false;
foreach (var ser in chart3.Series)
{
if (str == ser.Name) //series already exists
{
doNotaddSeries = true;
ser.Points.AddY(1);
//MessageBox.Show(str + " exists");
break;
}
}
if (!doNotaddSeries)
{
chart3.Series.Add(str);
chart3.Series[str].Points.AddY(1);
}
doNotaddSeries = false;
}
}
我想要的是(可以說)如果我有:
str[0]="abc"
str[1]="def"
str[2]="abc"
我希望“abc”在單個條上表示,y 上的值為 2。 而“def”在 y 軸上應該有 1 個值。 我得到的是“abc”被表示為 2 個不同的條,但圖例中的顏色表示類似
只需使用 linq GroupBy 為您處理它然后添加它們
var DTCs = new [] {"abc", "def", "abc"};
var points = DTCs.GroupBy(str => str).Select(group => new
{
x = group.Key,
y = group.Count()
});
// loop through points adding them to the chart
foreach (var p in points)
{
// Assume this is correct for your chart object
chart3.Series.Add(p.x);
chart3.Series[p.x].Points.AddY(p.y);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.