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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 26, 2014, 12:08 PM
VulpesPosted Mar 27, 2014, 12:40 PM
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.
Farhan ShariffPosted Mar 27, 2014, 11:35 AM
Farhan ShariffPosted Mar 27, 2014, 11:32 AM
VulpesPosted Mar 27, 2014, 11:27 AM
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 ShariffPosted Mar 27, 2014, 10:02 AM
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();
}
Farhan ShariffPosted Mar 26, 2014, 5:04 PM
VulpesPosted Mar 26, 2014, 1:31 PM
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 ShariffPosted Mar 26, 2014, 12:49 PM
VulpesPosted Mar 26, 2014, 12:47 PM
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 ShariffPosted Mar 26, 2014, 12:16 PM
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");
}
}
}
Farhan ShariffPosted Mar 26, 2014, 12:11 PM
Abhinav AbhishekPosted Mar 26, 2014, 12:08 PM
Farhan ShariffPosted Mar 26, 2014, 11:29 AM
I want X axis as 0 1 2 3 4.. X axis scale is values in Cpk column
VulpesPosted Mar 26, 2014, 11:20 AM
chart1.Series["Series2"].Points.DataBindXY(csvData.DefaultView, "CpK", csvData.DefaultView, "Delta_Sigma");
Farhan ShariffPosted Mar 26, 2014, 10:21 AM
chart1.DataSource = csvData;
chart1.Series["Series2"].YValueMembers = "Delta_Sigma";
chart1.Series["Series2"].XValueMember = "CpK";
chart1.DataBind();
Abhay ShankerPosted Mar 26, 2014, 6:33 AM
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