[英]C# data chart in x axis shows multiple times cities windows form
上师请把这个可怜的padawan切碎。
我正在尝试显示一个图表,其中包含我在数据库中提供的值。 例如,在X轴上,我只想要一个名为“鹿特丹”的城市,但它在x轴上与其他行相比给了我鹿特丹很多倍。
这是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'official_DatabaseDataSet.fietsdiefstal' table. You can move, or remove it, as needed.
this.fietsdiefstalTableAdapter.Fill(this.official_DatabaseDataSet.fietsdiefstal);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
chart1.Series["Fietsdiefstal"].XValueMember = "Plaats";
chart1.Series["Fietsdiefstal"].YValueMembers = "Begintijd";
chart1.DataSource = official_DatabaseDataSet.fietsdiefstal;
chart1.DataBind();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
fietsdiefstalBindingSource.EndEdit();
fietsdiefstalTableAdapter.Update(official_DatabaseDataSet.fietsdiefstal);
MessageBox.Show("Your data has been succesfully saved.", " Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void chart1_Click(object sender, EventArgs e)
{
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.fietsdiefstalTableAdapter.FillBy(this.official_DatabaseDataSet.fietsdiefstal);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void time1300ToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.fietsdiefstalTableAdapter.Time1300(this.official_DatabaseDataSet.fietsdiefstal);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void timeToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.fietsdiefstalTableAdapter.Time(this.official_DatabaseDataSet.fietsdiefstal, param1ToolStripTextBox.Text);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void param1ToolStripLabel_Click(object sender, EventArgs e)
{
}
private void straatToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.fietsdiefstalTableAdapter.Straat(this.official_DatabaseDataSet.fietsdiefstal, param1ToolStripTextBox1.Text);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}
因此,似乎您正在为数据网格和图表使用fietsdiefstal
数据。
您应该创建第二个数据集,以将结果聚合为所需的格式。 您可以使用C#或您用来填充数据表的任何语言来完成此操作(我假设使用SQL?)
因此,例如,在SQL中,查询应类似于
SELECT City, SUM(SomeValue) FROM MyTable
GROUP BY City
您将在其中使用哪个City
填充X轴,而条形图高度将是SomeValue
。
如果要在C#中执行此操作,则需要遍历记录。 就像是:
var aggregateData = new Dictionary<string, int>();
foreach(DataRow row in YourDataTable.Rows)
{
int val;
aggregateData.TryGetValue(row["City"], out val);
aggregateData[row["City"]] = val + row["SomeValue"];
}
(我尚未测试这些示例)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.