I have a DataTable with 21 columns.
I want to plot a graph of Cpk(column name)(X axis ) and Delta_sigama(column name)(y axis).
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2014, 8:08 AM EDT
Abhay ShankerRank #375 · 6.7K reputationPosted Mar 26, 2014, 2:33 AM EDT
http://blogs.msdn.com/b/alexgor/archive/2009/02/21/data-binding-ms-chart-control.aspx
http://www.etechpulse.com/2013/04/how-to-create-chart-using-data-table.html
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 26, 2014, 6:21 AM EDT
chart1.DataSource = csvData;
chart1.Series["Series2"].YValueMembers = "Delta_Sigma";
chart1.Series["Series2"].XValueMember = "CpK";
chart1.DataBind();
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2014, 7:20 AM EDT
chart1.Series["Series2"].Points.DataBindXY(csvData.DefaultView, "CpK", csvData.DefaultView, "Delta_Sigma");
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 26, 2014, 7:29 AM EDT
I want X axis as 0 1 2 3 4.. X axis scale is values in Cpk column
Abhinav AbhishekRank #2,198 · 642 reputationPosted Mar 26, 2014, 8:08 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 26, 2014, 8:11 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 26, 2014, 8:16 AM EDT
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;
using System.Windows.Forms.DataVisualization.Charting;
using Microsoft.VisualBasic.FileIO;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private DataTable csvData;
public Form1()
{
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// To list only csv files, we need to add this filter
openFileDialog.Filter = "|*.csv";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
tbCSVPath.Text = openFileDialog.FileName;
}
csvData = GetDataTableFromCSVFile(tbCSVPath.Text);
}
private static DataTable GetDataTableFromCSVFile(string csvfilePath)
{
DataTable csvData = new DataTable();
using (TextFieldParser csvReader = new TextFieldParser(csvfilePath))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
//Read columns from CSV file, remove this line if columns not exits
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
return csvData;
}
private void button2_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisY.Maximum = 10;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.Series["Series2"].Points.DataBindXY(csvData.DefaultView, "CpK_Refrence", csvData.DefaultView, "Delta_Sigma");
}
}
}
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2014, 8:47 AM EDT
If you use Chart.DataSource then you can use a DataTable.
However, if you're using DataPointCollection.DataBindXY, then you have to use a DataView instead because the data-source needs to implement IEnumerable which DataTable doesn't.
I can't tell from the code what the ChartType is set to, so it must be using the setting in the Properties box.
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 26, 2014, 8:49 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2014, 9:31 AM EDT
Presumably, all the infinite values are gathered together on the x-axis.
What you could try is to create an additional column in the datatable and replace the infinite values with some reasonable finite maximum (or minimum if they're negative infinity) or, otherwise, just copy the values across. You could then bind to the additional column.
Alternatively, you could create a DefaultView and then filter out those rows which have infinite values in them.
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 26, 2014, 1:04 PM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 27, 2014, 6:02 AM EDT
The issue is that when you are reading the data from the csv it is adding all the data as string and the chart is then reading the values as string thats why none of the Chart1.ChartAreas(0).AxisX.Maximum = 10 changes the axis.
The column type have to be changed so
private void button2_Click(object sender, EventArgs e)
{
DataTable dtCloned = csvData.Clone();
dtCloned.Columns["Delta_Sigma"].DataType = typeof(double);
dtCloned.Columns["CpK_Refrence"].DataType = typeof(double);
//dtCloned.Columns[3].DataType = typeof(double);
foreach (DataRow row in csvData.Rows)
{
dtCloned.ImportRow(row);
}
chart1.DataSource = dtCloned;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Minimum = -5;
chart1.ChartAreas[0].AxisX.Maximum = 5;
chart1.Series["Series2"].YValueMembers = "Delta_Sigma";
chart1.Series["Series2"].XValueMember = "CpK_Refrence";
chart1.DataBind();
}
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2014, 7:27 AM EDT
I still can't even with the revised code in your last post :(
Could you perhaps attach an image of what the chart looks like now.
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 27, 2014, 7:32 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted Mar 27, 2014, 7:35 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2014, 8:40 AM EDT
As far as changing the background color of the chart is concerned if, in you look at the ChartAreas collection in the Properties Box, you can set the BackColor property of each ChartArea.
There's also a secondary BackColor you can set for gradient or hatching purposes.