I am building an Asp.net Stacked Column chart.
Here is how it looks :
Here is how it should look :
Ignore the numbers on the chart but look at the X axis - Why does it give me 1148,1153, 1163 when they do not appear in the data.
Here is my Data :
Here is the code:
Dim chart As New Chart
chart.ID = "Chart1"
Dim chartareas As New ChartArea
chart.ChartAreas.Add(chartareas)
chart.DataBindCrossTable(DtFinalRecords.DefaultView, "OutcomeScore", "TermID", "RecordsPerGroup", "Label=RecordsPerGroup")
chart.ChartAreas(0).AxisX.MajorGrid.Enabled = False
chart.ChartAreas(0).AxisY.MajorGrid.Enabled = False
For Each cs As Series In chart.Series
cs.ChartType = SeriesChartType.StackedColumn
Next
pnlcharts.Controls.Add(chart)
Any help would be appreciated. Thank you!
DataBindCrossTable does the best job it can with minimal coding effort required from you. But if you are not happy with the default behavior then you have to explicitly customize it. In your particular case, you want to assign custom labels to your data points:
protected void Page_Load(object sender, EventArgs e)
{
Chart1.Palette = ChartColorPalette.None;
Chart1.PaletteCustomColors = new Color[] { ColorTranslator.FromHtml("#DF5B59"), ColorTranslator.FromHtml("#E0D773 "), ColorTranslator.FromHtml("#8AAC53"), ColorTranslator.FromHtml("#6A843F") };
Chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
Chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
Chart1.ChartAreas[0].AxisX.Interval = 1;
var rows = from row in dt.AsEnumerable() select row.Field<int>("OutcomeScore");
Chart1.Series.Clear();
foreach (int i in rows.Distinct())
Chart1.Series.Add(new Series { Name = i.ToString(), ChartType = SeriesChartType.StackedColumn });
foreach (DataRow dr in dt.Rows)
{
DataPoint dp = new DataPoint();
dp.AxisLabel = dr["TermID"].ToString();
dp.Label = dr["RecordsPerGroup"].ToString();
dp.XValue = (int)dr["TermID"];
dp.YValues[0] = (int)dr["RecordsPerGroup"];
string name = dr["OutcomeScore"].ToString();
Chart1.Series[name].Points.Add(dp);
}
}
Related
I have a Datagridview which is connected with excel sheets. My Excel sheet is a good receipt which contains the quantity of the items. Now i want to print labels as much as the amount. E.g if i have 10 T shirts, i want to print 10 labels with a 1 in quantity.
I've already tried the following code.
private void btnSingle_Click(object sender, EventArgs e)
{
BindingList<LableClass> GoodReceipt= new BindingList<LableClass>();
GoodReceipt.Add(new LableClass()
{
Charge = dataGridView1.SelectedCells[0].Value.ToString(),
ItemNr= dataGridView1.SelectedCells[1].Value.ToString(),
Description= dataGridView1.SelectedCells[2].Value.ToString(),
Quantity= float.Parse(dataGridView1.SelectedCells[8].Value.ToString())
});
LableClassBindingSource.DataSource = GoodReceipt;
PrinterSettings prtSetting = new PrinterSettings();
foreach (PaperSize item in prtSetting.PaperSizes)
{
if (item.PaperName == "62mm x 29mm")
prtSetting.DefaultPageSettings.PaperSize = item;
}
PageSettings ps = new PageSettings(prtSetting);
ps.Landscape = false;
ps.Margins.Top = 8;
ps.Margins.Bottom = 0;
ps.Margins.Left = 6;
ps.Margins.Right = 0;
PrintDocument prtDoc = new PrintDocument();
prtDoc.PrinterSettings = prtSetting;
prtSetting.Copies = 5;
prtSetting.FromPage = 1;
prtSetting.ToPage = 5;
reportViewer1.SetPageSettings(ps);
prtDoc.Print();
reportViewer1.RefreshReport();
}
I tried to print 5 labels for a test, but somehow all of them are empty. Sorry for bad english.
I'm working on a chart in asp.net that is dinamically created from a DataTable. Basically I've got a DataTable like this:
So, what I want in my chart is a column for each differente location, and for each diferent Type I want to have a "portion" of the column, meaning I would like to have a column per location divided per type.
I have managed to do something like this, but not quite what I would like:
Now I'll try to explain my code.
First, i add the different locations in my datatable to a list of strings
string loc = "";
for (int i = 0; i < dtTipos.Rows.Count; i++)
{
if (loc != dtTipos.Rows[i]["Location"].ToString())
listaLocais.Add(dtTipos.Rows[i]["Location"].ToString());
loc = dtTipos.Rows[i]["Location"].ToString();
}
After, for each different location, I select the rows to the corresponding location and create a series for each one of them. I proceed to create a point with y=0 and one with y=total
for (int i = 0; i < listaLocais.Count; i++)
{
DataRow[] lRow = dtTipos.Select("Location = '" + listaLocais[i] + "'");
foreach (DataRow dr in lRow)
{
string serie = dr["Location"].ToString().Substring(0, 2) + "_" + dr["Type"].ToString() + " - " + dr["Total"].ToString();
Series s = new Series();
s.Name = serie;
s.Label = dr["Location"].ToString();
s.ChartType = SeriesChartType.StackedColumn;
chartTipo1.Series.Add(s);
s.Points.AddXY(i + 1, 0);
s.Points[0].Label = " ";
s.Points.AddXY(i + 1,Convert.ToInt32(dr["Total"].ToString()));
s.Points[1].Label = " ";// dtTipos.Rows[i]["Total"].ToString();
}
}
The thing is: I think that because I create a serie for each row, everytime I use a diferent column, the y axis starts from where I left it.
Is there any way so I can get this to work?
I'll appreciate any help.
You're adding your series and data points in the same loop. First add your series, then your data points:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new MyDataTable();
//first add your series
foreach (DataRow row in dt.DefaultView.ToTable(true, new string[] { "Type" }).Rows)
{
Series series = new Series();
series.Name = (string)row["Type"];
series.ChartType = SeriesChartType.StackedColumn;
Chart1.Series.Add(series);
}
// then add your points;
foreach (DataRow row in dt.Rows)
Chart1.Series[(string)row["Type"]].Points.AddXY(row["Location"], new object[] { row["Total"] });
}
}
I have a WindowsForms application that I am compiling as a DLL to be used by another program. This program supplies the DLL with an array filled with information(floats). What I want to do is display these numbers in an organized way(row and columns). The methods I've investigated so far incorporate the use of a DataGridView object, however I do not have a "database", I only have the raw data stored as an array.
Would the only way to do this involve creating a SQL database with my data from the array? Or is there an easier/faster way of doing this?
Thanks for the replies, but I just ended up looping through the creation of the rows and columns and filling them with my required data.
Creating/initializing DataGridView:
private System.Windows.Forms.DataGridView dataGridView1;
this.dataGridView1 = new System.Windows.Forms.DataGridView();
//Size and location can be changed to whatever you want
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 436);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 70; //Adjusts row header to fit entire "Score" string
this.dataGridView1.Size = new System.Drawing.Size(976, 398);
this.dataGridView1.TabIndex = 10;
Creating rows/columns and filling them with my data:
for (int i = 0; i <= 40; i++)
{
dataGridView1.Columns.Add("column" + i.ToString(), i.ToString());
dataGridView1.Columns[i].Width = 22;
}
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
for (int i = 0; i <= 40; i++)
{
row.Cells[i].Value = scores[0,i]; // "scores" is an Int32[,] array filled with my data
}
dataGridView1.Rows.Add(row);
dataGridView1.Rows[0].HeaderCell.Value = "Score";
The result looked something like this.
You can send the gridview data in XML format
private void Form1_Load(object sender, EventArgs e) {
DataSet ds = new DataSet();
ds.ReadXml(#"XMLFile1.xml");
gridControl1.DataSource = ds.Tables[1];
gridView1.PopulateColumns();
}
As you can clearly see the XAMDataChart is skipping putting the label on every other column.
I have no clue why this is happening. The code is the same for all of them and I have verified that the data is there.
Here is how they are generated
CategoryXAxis catX = new CategoryXAxis();
NumericYAxis numY = new NumericYAxis();
foreach (var series in control.masterCollection)
{
catX.Name = "catX";
catX.ItemsSource = series;
catX.Label = "{Label}";
catX.Gap = 20;
numY.Name = "numY";
ColumnSeries cs = new ColumnSeries()
{
ItemsSource = series,
ValueMemberPath = "YPoint",
XAxis = catX,
YAxis = numY
};
}
The answer to this question is setting the Category X Axis objects interval to 1.
catX.Interval = 1;
I have 2 problems:
I want the names from the datatable but it is showing me in numeric form.
I would like a gap between the two bars but I can't find a way.
Here is the code:
private void InitializeGraph (DataTable poDt)
{
Telerik.Charting.ChartSeries chartseries = new Telerik.Charting.ChartSeries();
try
{
chartseries.Type = Telerik.Charting.ChartSeriesType.Bar;
Telerik.Charting.ChartSeriesItem csItem;
RadChart1.PlotArea.XAxis.AutoScale = true;
RadChart1.PlotArea.XAxis.DataLabelsColumn = "Name";
for (int iRow = 0; iRow < poDt.Rows.Count; iRow++)
{
chartseries = new Telerik.Charting.ChartSeries();
chartseries.Type = Telerik.Charting.ChartSeriesType.Bar;
chartseries.Name = poDt.Rows[iRow]["Name"].ToString().Trim();
csItem = new Telerik.Charting.ChartSeriesItem();
csItem.Name = poDt.Rows[iRow]["Name"].ToString();
csItem.Label.TextBlock.Text = poDt.Rows[iRow]["Value"].ToString();
RadChart1.PlotArea.XAxis.Appearance.TextAppearance.AutoTextWrap = Telerik.Charting.Styles.AutoTextWrap.True;
csItem.YValue = Int32.Parse(poDt.Rows[iRow]["Value"].ToString());
chartseries.AddItem(csItem);
RadChart1.Series.Add(chartseries);
}
RadChart1.PlotArea.XAxis.AddRange(1, poDt.Rows.Count, 1);
RadChart1.PlotArea.XAxis[poDt.Rows.Count].TextBlock.Text = chartseries.Name;
poDt.Rows.Count.ToString();
RadChart1.PlotArea.XAxis.AutoShrink = false;
RadChart1.PlotArea.XAxis.AutoShrink = true;
RadChart1.Series.Add(chartseries);
RadChart1.PlotArea.Appearance.Border.Visible = false;
RadChart1.Appearance.Border.Visible = true;
RadChart1.PlotArea.YAxis.IsLogarithmic = true;
RadChart1.PlotArea.YAxis.AutoScale = true;
RadChart1.PlotArea.YAxis.Appearance.ValueFormat=Telerik.Charting.Styles.ChartValueFormat.Number;
RadChart1.Appearance.BarWidthPercent = 50;
RadChart1.Chart.Appearance.FillStyle.MainColor = System.Drawing.Color.Red;
RadChart1.Chart.Appearance.FillStyle.MainColor = System.Drawing.Color.Transparent;
RadChart1.Legend.Appearance.FillStyle.MainColor = System.Drawing.Color.Transparent;
}
catch (Exception Ex)
{
//throw;
}
finally
{
poDt.Clear();
poDt = null;
chartseries = null;
}
}
Sorry, I do not believe there is a way to display two X-axis at the same time.
My suggestion is you use a CategoricalAxis for your X axis and create a custom bar chart that has a legend which differentiates the two values. I don't have any working samples, however you can use this Telerik Silverlight demo for starters.
Also, switch to RadChartView if you can. Because I would then suggest an easier approach, which is using a Categorical X-Axis and create multiple Y axes. If you go that route, you can do something like this for a DateTimeContinuous (or Categorical) X-axis with multiple Y-axes :
int count = 0;
LineSeries lineSeries = new LineSeries();
lineSeries.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "TimeStamp" };
lineSeries.ValueBinding = new PropertyNameDataPointBinding() { PropertyName = "Value" };
lineSeries.VerticalAxis = new LinearAxis()
{
Title = "Title Here"
};
lineSeries.ItemsSource = yourCollection.Values;
//First Y-axis to be placed on the left of X-axis,
//additional Y-axes to be placed on right
if (count > 0 )
{
lineSeries.VerticalAxis.HorizontalLocation = Telerik.Charting.AxisHorizontalLocation.Right;
}
count++;
chartName.Series.Add(lineSeries);
Hope this helps.