PDA

View Full Version : رسم نمودار از داده های ورودی



soheil.star
جمعه 25 آذر 1390, 19:35 عصر
سلام دوستان
چه جوری میشه از داده های ورودی یعنی اطلاعاتی که یوزر وارد میکنه نمودار تهیه کرد؟
مثلا چندین چک باکس داریم که کاربر عدد وارد اونا میکنه و می خوایم برای داده های ورودی نمودار خود به خود تشکیل بشه
از chart در ویژوال استدیو 2010 استفاده کردم ولی اون نمودار رو اماده می سازه در صورتی که من می خوام بعد از وارد کردن اعداد در صورت کلیک روی لینک مربوط نمودار ظاهر بشه.

ممنون میشم کمک کنید

secondstriker
جمعه 25 آذر 1390, 20:38 عصر
یکی از راه هایی که وجود داره اینه که دیتا ها رو به excell منتقل کنی و در آنجا از دیتاها نمودار بگیری. هر کاری که با excell می تونی انجام بدی از تو خود نرم افزار می تونی دستورشو بنویسی که تو excell برات انجام بشه!
توی یه کلاس اینو بنویس. اما قبلش توی رفرنسای کلاس اینو از تو رفرنسای دات نت اضافه کن: Microsoft.Office.Interop.Excel
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
namespace ExcelExample
{
class CreateExcelDoc
{
private Application app = null;
private Workbook workbook = null;
private Worksheet worksheet = null;
private Range workSheet_range = null;
public CreateExcelDoc()
{
createDoc();
}
public void createDoc()
{
try
{
app = new Application();
app.Visible = true;
workbook = app.Workbooks.Add(1);
worksheet = (Worksheet)workbook.Sheets[1];
}
catch (Exception e)
{
Console.Write("Error");
}
finally
{
}
}

public void createHeaders(int row, int col, string htext, string cell1,
string cell2, int mergeColumns,string b, bool font,int size,string
fcolor)
{
worksheet.Cells[row, col] = htext;
workSheet_range = worksheet.get_Range(cell1, cell2);
workSheet_range.Merge(mergeColumns);
switch(b)
{
case "YELLOW":
workSheet_range.Interior.Color = System.Drawing.Color.Yellow.ToArgb();
break;
case "GRAY":
workSheet_range.Interior.Color = System.Drawing.Color.Gray.ToArgb();
break;
case "GAINSBORO":
workSheet_range.Interior.Color =
System.Drawing.Color.Gainsboro.ToArgb();
break;
case "Turquoise":
workSheet_range.Interior.Color =
System.Drawing.Color.Turquoise.ToArgb();
break;
case "PeachPuff":
workSheet_range.Interior.Color =
System.Drawing.Color.PeachPuff.ToArgb();
break;
default:
// workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
break;
}

workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
workSheet_range.Font.Bold = font;
workSheet_range.ColumnWidth = size;
if (fcolor.Equals(""))
{
workSheet_range.Font.Color = System.Drawing.Color.White.ToArgb();
}
else {
workSheet_range.Font.Color = System.Drawing.Color.Black.ToArgb();
}
}

public void addData(int row, int col, string data,
string cell1, string cell2,string format)
{
worksheet.Cells[row, col] = data;
workSheet_range = worksheet.get_Range(cell1, cell2);
workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
workSheet_range.NumberFormat = format;
}
}
}

بعد توی مین برنامه هم با این دستورات می تونی دیتا ها رو به excell اضافه کنی:

CreateExcelDoc excell_app = new CreateExcelDoc();
//creates the main header
excell_app.createHeaders(5, 2, "Total of Products", "B5", "D5", 2,"YELLOW",true,10,"n");
//creates subheaders
excell_app.createHeaders(6, 2, "Sold Product", "B6", "B6", 0, "GRAY", true,10,"");
excell_app.createHeaders(6, 3, "", "C6", "C6", 0, "GRAY", true,10,"");
excell_app.createHeaders(6, 4, "Initial Total", "D6", "D6", 0, "GRAY", true,10,"");
//add Data to cells
excell_app.addData(7, 2, "114287", "B7", "B7","#,##0");
excell_app.addData(7, 3, "", "C7", "C7", "");
excell_app.addData(7, 4, "129121", "D7", "D7", "#,##0");
//add percentage row
excell_app.addData(8, 2, "", "B8", "B8", "");
excell_app.addData(8, 3, "=B7/D7", "C8", "C8", "0.0%");
excell_app.addData(8, 4, "", "D8", "D8", "");
//add empty divider
excell_app.createHeaders(9, 2, "", "B9", "D9", 2, "GAINSBORO", true, 10, "");

اینا توی سایت http://www.codeproject.com/KB/cs/Excel_and_C_.aspx بود که غلط داشت که برات درستش کردم.
اما این دستورات فقط دیتا اضافه می کنه نمودار نمیکشه اگه اونم پیدا کردم برات میزارم همین جا.:لبخندساده:

secondstriker
جمعه 25 آذر 1390, 21:33 عصر
اینم از رسم نمودار از #C در Excel::چشمک:

اما قبلش توی رفرنسا اینو از تو رفرنسای دات نت اضافه کن: Microsoft.Office.Interop.Excel
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Example9_7Q
{
public partial class Form1 : Form
{
private Microsoft.Office.Interop.Excel.Application xla=null;

public Form1()
{
InitializeComponent();

}


private double[,] AddData(int nRows, int nColumns)
{
double[,] dataArray = new double[nRows, nColumns];
double[] xarray = new double[nColumns];
for (int i = 0; i < xarray.Length; i++)
{
xarray[i] = -3.0f + i * 0.25f;
}
double[] yarray = xarray;

for (int i = 0; i < dataArray.GetLength(0); i++)
{
for (int j = 0; j < dataArray.GetLength(1); j++)
{
dataArray[i, j] = 3 * Math.Pow((1 - xarray[i]), 2)
* Math.Exp(-xarray[i] * xarray[i] -
(yarray[j] + 1) * (yarray[j] + 1)) -
10 * (0.2 * xarray[i] - Math.Pow(xarray[i], 3) -
Math.Pow(yarray[j], 5)) *
Math.Exp(-xarray[i] * xarray[i] - yarray[j] * yarray[j])
- 1 / 3 * Math.Exp(-(xarray[i] + 1) * (xarray[i] + 1) -
yarray[j] * yarray[j]);
}
}
return dataArray;
}



private void button1_Click(object sender, EventArgs e)

{
xla = new Microsoft.Office.Interop.Excel.Application();

xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;

// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
Chart xlChart = chartObj.Chart;

int nRows = 25;
int nColumns = 25;
string upperLeftCell = "B3";
int endRowNumber = System.Int32.Parse(upperLeftCell.Substring(1))
+ nRows - 1;
char endColumnLetter = System.Convert.ToChar(
Convert.ToInt32(upperLeftCell[0]) + nColumns - 1);
string upperRightCell = System.String.Format("{0}{1}",
endColumnLetter, System.Int32.Parse(upperLeftCell.Substring(1)));
string lowerRightCell = System.String.Format("{0}{1}",
endColumnLetter, endRowNumber);

// Send single dimensional array to Excel:
Range rg1 = ws.get_Range("B2", "Z2");
double[] xarray = new double[nColumns];
ws.Cells[1, 1] = "Data for surface chart";
for (int i = 0; i < xarray.Length; i++)
{
xarray[i] = -3.0f + i * 0.25f;
ws.Cells[i + 3, 1] = xarray[i];
ws.Cells[2, 2 + i] = xarray[i];
}

Range rg = ws.get_Range(upperLeftCell, lowerRightCell);
rg.Value2 = AddData(nRows, nColumns);

Range chartRange = ws.get_Range("A2", lowerRightCell);
xlChart.SetSourceData(chartRange, Type.Missing);
xlChart.ChartType = XlChartType.xlSurface;

// Customize axes:
Axis xAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory,
XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "X Axis";

Axis yAxis = (Axis)xlChart.Axes(XlAxisType.xlSeriesAxis,
XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Y Axis";

Axis zAxis = (Axis)xlChart.Axes(XlAxisType.xlValue,
XlAxisGroup.xlPrimary);
zAxis.HasTitle = true;
zAxis.AxisTitle.Text = "Z Axis";

// Add title:
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = "Peak Function";

// Remove legend:
xlChart.HasLegend = false;

/* This following code is used to create Excel default color indices:
for (int i = 0; i < 14; i++)
{
string cellString = "A" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = i + 1;
ws.get_Range(cellString, cellString).Value2 = i + 1;
cellString = "B" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = 14 + i + 1;
ws.get_Range(cellString, cellString).Value2 = 14 + i + 1;
cellString = "C" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = 2 * 14 + i + 1;
ws.get_Range(cellString, cellString).Value2 = 2 * 14 + i + 1;
cellString = "D" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = 3 * 14 + i + 1;
ws.get_Range(cellString, cellString).Value2 = 3 * 14 + i + 1;
}*/
}

private void button2_Click(object sender, EventArgs e)
{
xla = new Microsoft.Office.Interop.Excel.Application();
xla.DisplayAlerts = false;
if (xla != null)
{
xla.Quit();
xla = null;
}
this.Close();
}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}
متن اصلی در http://www.codeproject.com/KB/cs/ExcelCSharp.aspx می باشد که اصلاحات آن در کد بالا انجام شد:لبخندساده:

soheil.star
دوشنبه 28 آذر 1390, 10:30 صبح
ممنون دوست گلم
تست می کنم و نتیجه رو میگم

soheil.star
دوشنبه 28 آذر 1390, 13:03 عصر
اینم از رسم نمودار از #C در Excel::چشمک:

اما قبلش توی رفرنسا اینو از تو رفرنسای دات نت اضافه کن: Microsoft.Office.Interop.Excel
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Example9_7Q
{
public partial class Form1 : Form
{
private Microsoft.Office.Interop.Excel.Application xla=null;

public Form1()
{
InitializeComponent();

}


private double[,] AddData(int nRows, int nColumns)
{
double[,] dataArray = new double[nRows, nColumns];
double[] xarray = new double[nColumns];
for (int i = 0; i < xarray.Length; i++)
{
xarray[i] = -3.0f + i * 0.25f;
}
double[] yarray = xarray;

for (int i = 0; i < dataArray.GetLength(0); i++)
{
for (int j = 0; j < dataArray.GetLength(1); j++)
{
dataArray[i, j] = 3 * Math.Pow((1 - xarray[i]), 2)
* Math.Exp(-xarray[i] * xarray[i] -
(yarray[j] + 1) * (yarray[j] + 1)) -
10 * (0.2 * xarray[i] - Math.Pow(xarray[i], 3) -
Math.Pow(yarray[j], 5)) *
Math.Exp(-xarray[i] * xarray[i] - yarray[j] * yarray[j])
- 1 / 3 * Math.Exp(-(xarray[i] + 1) * (xarray[i] + 1) -
yarray[j] * yarray[j]);
}
}
return dataArray;
}



private void button1_Click(object sender, EventArgs e)

{
xla = new Microsoft.Office.Interop.Excel.Application();

xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;

// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
Chart xlChart = chartObj.Chart;

int nRows = 25;
int nColumns = 25;
string upperLeftCell = "B3";
int endRowNumber = System.Int32.Parse(upperLeftCell.Substring(1))
+ nRows - 1;
char endColumnLetter = System.Convert.ToChar(
Convert.ToInt32(upperLeftCell[0]) + nColumns - 1);
string upperRightCell = System.String.Format("{0}{1}",
endColumnLetter, System.Int32.Parse(upperLeftCell.Substring(1)));
string lowerRightCell = System.String.Format("{0}{1}",
endColumnLetter, endRowNumber);

// Send single dimensional array to Excel:
Range rg1 = ws.get_Range("B2", "Z2");
double[] xarray = new double[nColumns];
ws.Cells[1, 1] = "Data for surface chart";
for (int i = 0; i < xarray.Length; i++)
{
xarray[i] = -3.0f + i * 0.25f;
ws.Cells[i + 3, 1] = xarray[i];
ws.Cells[2, 2 + i] = xarray[i];
}

Range rg = ws.get_Range(upperLeftCell, lowerRightCell);
rg.Value2 = AddData(nRows, nColumns);

Range chartRange = ws.get_Range("A2", lowerRightCell);
xlChart.SetSourceData(chartRange, Type.Missing);
xlChart.ChartType = XlChartType.xlSurface;

// Customize axes:
Axis xAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory,
XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "X Axis";

Axis yAxis = (Axis)xlChart.Axes(XlAxisType.xlSeriesAxis,
XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Y Axis";

Axis zAxis = (Axis)xlChart.Axes(XlAxisType.xlValue,
XlAxisGroup.xlPrimary);
zAxis.HasTitle = true;
zAxis.AxisTitle.Text = "Z Axis";

// Add title:
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = "Peak Function";

// Remove legend:
xlChart.HasLegend = false;

/* This following code is used to create Excel default color indices:
for (int i = 0; i < 14; i++)
{
string cellString = "A" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = i + 1;
ws.get_Range(cellString, cellString).Value2 = i + 1;
cellString = "B" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = 14 + i + 1;
ws.get_Range(cellString, cellString).Value2 = 14 + i + 1;
cellString = "C" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = 2 * 14 + i + 1;
ws.get_Range(cellString, cellString).Value2 = 2 * 14 + i + 1;
cellString = "D" + (i + 1).ToString();
ws.get_Range(cellString, cellString).Interior.ColorIndex = 3 * 14 + i + 1;
ws.get_Range(cellString, cellString).Value2 = 3 * 14 + i + 1;
}*/
}

private void button2_Click(object sender, EventArgs e)
{
xla = new Microsoft.Office.Interop.Excel.Application();
xla.DisplayAlerts = false;
if (xla != null)
{
xla.Quit();
xla = null;
}
this.Close();
}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}
متن اصلی در http://www.codeproject.com/KB/cs/ExcelCSharp.aspx می باشد که اصلاحات آن در کد بالا انجام شد:لبخندساده:

فقط یه سوال: اگه از همین کد که تو پسن دوم خودتون گزاشتین استفاده کنم کافیه؟و نیازی به کد پست اول نیست؟
و این رو باید در button ی که می خوام نمودار بگیره بزارم دیگه؟