Consider an ASP.NET MVC controller method that builds an ASP.NET Chart image.
public FileStreamResult MakeImg(IEnumerable<MyObj> stats)
{
Chart barchart = BarChart(400, 300);
Series series1 = new Series("Series1");
series1.ChartArea = "ca1";
series1.ChartType = SeriesChartType.Column;
series1.IsValueShownAsLabel = true;
series1.Font = new Font("Verdana", 9f, FontStyle.Regular);
barchart.Series.Add(series1);
// Set chart data source
barchart.DataSource = stats;
// Set series members names for the X and Y values
barchart.Series["Series1"].XValueMember = "FriendlyDate";
barchart.Series["Series1"].YValueMembers = "NumRecords";
// Data bind to the selected data source
barchart.DataBind();
MemoryStream ms = new MemoryStream();
barchart.SaveImage(ms, ChartImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(ms, "image/png");
}
The image is rendered in an unattractive manner:
fugly http://www.imagechicken.com/uploads/1253830647005451400.png
Question: How can I set the font programmatically for the:
X and Y axis labels - i.e. 0 through 35 on Y, and the dates on X
data - i.e. 12, 0, 0, 3, 6 ?
chart.ChartAreas[0].AxisX.LabelStyle.Font
chart.ChartAreas[0].AxisY.LabelStyle.Font
is the property you need to set the font for the Axes.
Chart1.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Verdana", 8f);
Chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = System.Drawing.Color.Red;
Another problem I faced was the jaggies on the text. Changing from .png to .jpg did the trick!
Related
I need to set the Legend position of Graph to Top left side. But not able to set as no such property exists on eLegendPosition.
var myChart = myWorksheet.Drawings.AddChart("Chart2", eChartType.ColumnClustered) as ExcelBarChart;
**Adding an series to chart**
var serie2a = myChart.Series.Add(myWorksheet.Cells[2, 2, dataRow, 2], myWorksheet.Cells[2, 1, dataRow, 1]);
//serie2a.Header = myWorksheet.Cells[1, 2].Value.ToString();
serie2a.Header = "Plan";
myChart.YAxis.Title.Text = "Daily MH %";
myChart.YAxis.Title.Font.Size = 8;
chart2b.UseSecondaryAxis = true; //Flip the axes
chart5e.UseSecondaryAxis = true; //Flip the axes
chart2b.YAxis.Title.Text = "Cumul Daily MH %";
chart2b.YAxis.Title.Font.Size = 8;
**Here I am Facing an issue while setting it to TopLeft**
myChart.Legend.Position = eLegendPosition.Bottom;
myChart.Border.Fill.Color = System.Drawing.Color.Green;
myChart.Title.Text = "OVERALL S CURVE";
myChart.Title.Font.Size = 10;
myChart.SetSize(800, 400);
// Add to 7th row and to the 7th column
myChart.SetPosition(7, 0, 7, 0);
The enum that Epplus has matches excel so that setting alone will not do what you need. But you could manually set it in XML using a ManualLayout node:
https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.drawing.charts.manuallayout?view=openxml-2.8.1
Similar to how it was done in How to add to Text Label For excel Charts using Open xml or EPPLUS.
Here is a helper method to make it a bit more concise:
/// <summary>
/// Sets the chart's layout node <c>c:chartSpace/c:chart/c:legend/c:layout</c> for the
/// passed <see cref="ExcelBarChart"/>.
/// </summary>
/// <param name="chart">Chart to set </param>
/// <param name="x">Position X Value (percentage).</param>
/// <param name="y">Position Y Value (percentage).</param>
/// <param name="culture">Culture to use when converting the doubles to string based on excel settings.</param>
public static void SetChartManualLayout(this ExcelBarChart chart, double x, double y, CultureInfo culture)
{
//Set layout via xml
var chartXml = chart.ChartXml;
var nsm = new XmlNamespaceManager(chartXml.NameTable);
var nsuri = chartXml.DocumentElement.NamespaceURI;
nsm.AddNamespace("c", nsuri);
nsm.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
//Get the title layout and add the manual section
var layoutNode = chartXml.SelectSingleNode("c:chartSpace/c:chart/c:legend/c:layout", nsm);
var manualLayoutNode = chartXml.CreateElement("c:manualLayout", nsuri);
layoutNode.AppendChild(manualLayoutNode);
//Add coordinates
var xModeNode = chartXml.CreateElement("c:xMode", nsuri);
var attrib = chartXml.CreateAttribute("val");
attrib.Value = "edge";
xModeNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(xModeNode);
var yModeNode = chartXml.CreateElement("c:yMode", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = "edge";
yModeNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(yModeNode);
var xNode = chartXml.CreateElement("c:x", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = x.ToString(culture);
xNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(xNode);
var yNode = chartXml.CreateElement("c:y", nsuri);
attrib = chartXml.CreateAttribute("val");
attrib.Value = x.ToString(culture);
yNode.Attributes.Append(attrib);
manualLayoutNode.AppendChild(yNode);
}
And to use it in your code, simply do this:
// Add to 7th row and to the 7th column
myChart.SetPosition(7, 0, 7, 0);
myChart.Legend.Position = eLegendPosition.Left;
myChart.SetChartManualLayout(0.015, 0.015, CultureInfo.CurrentCulture);
Provide the X and Y as percent. 0,0 would put it at the top-left and 1,1 would put it at the bottom-right. Also, make sure to account for Culture if Excel is set differently than your app.
The supposed value for this would is 15.2 sq cm. however, when annotation is moved, it becomes 0.04 sq cm.
I got this data from my application where area is computed as 9658.6572265625 and its calibration value is 0.00157889 which results in 15.2 value.
when the corresponding coordinates in pdf space are provided (see code below), the area computed is 5433.001953125.
so i compute the calibration value in pdf space like this.
ratio = area / pdfArea
pdfCalibrationValue = 0.00157889 * ratio;
the result is 0.002806911838696635. which if computed
5433.001953125 x 0.002806911838696635 = 15.2
so i am wondering why the result becomes 0.04.
Thoughts?
public class Test {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("src.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("result.pdf"));
Rectangle location = new Rectangle(426.582f, 514.291f, 559.0f, 613.818f);
float[] floats = new float[] {
427.582f, 582.873f,
493.036f, 515.291f,
558.0f, 554.237f,
527.4f, 612.818f,
464.727f, 564.709f,
427.582f, 582.873f
};
PdfArray pdfVertices= new PdfArray(floats);
float calib = 0.002806911838696635f;
PdfAnnotation stamp = PdfAnnotation.createPolygonPolyline(stamper.getWriter(),
location, "15.2 sq cm", true, new PdfArray(pdfVertices));
stamp.setColor(BaseColor.RED);
stamp.setBorderStyle(new PdfBorderDictionary(1, PdfBorderDictionary.STYLE_SOLID));
stamp.put(PdfName.SUBTYPE, PdfName.POLYGON);
stamp.put(new PdfName("IT"), new PdfName("PolygonDimension"));
stamp.put(PdfName.MEASURE, createMeasureDictionary(calib));
stamper.addAnnotation(stamp, 1);
stamper.close();
reader.close();
}
private static PdfDictionary createMeasureDictionary(float pdfCalibrationValue) {
String unit = "cm";
PdfDictionary measureDictionary = new PdfDictionary();
measureDictionary.put(PdfName.TYPE, PdfName.MEASURE);
measureDictionary.put(PdfName.R, new PdfString("1 " + unit + " = 1 " + unit));
PdfDictionary xDictionary = new PdfDictionary();
xDictionary.put(PdfName.TYPE, PdfName.NUMBERFORMAT);
xDictionary.put(PdfName.U, new PdfString(unit));
xDictionary.put(PdfName.C, new PdfNumber(pdfCalibrationValue));
PdfArray xarr = new PdfArray();
xarr.add(xDictionary);
measureDictionary.put(PdfName.X, xarr);
PdfDictionary dDictionary = new PdfDictionary();
dDictionary.put(PdfName.TYPE, PdfName.NUMBERFORMAT);
dDictionary.put(PdfName.U, new PdfString(unit));
dDictionary.put(PdfName.C, new PdfNumber(1));
PdfArray darr = new PdfArray();
darr.add(dDictionary);
measureDictionary.put(PdfName.D, darr);
PdfDictionary aDictionary = new PdfDictionary();
aDictionary.put(PdfName.TYPE, PdfName.NUMBERFORMAT);
aDictionary.put(PdfName.U, new PdfString("sq " + unit));
aDictionary.put(PdfName.C, new PdfNumber(1));
PdfArray aarr = new PdfArray();
aarr.add(aDictionary);
measureDictionary.put(PdfName.A, aarr);
return measureDictionary;
}
}
You have calculated a factor to translate the areas:
the result is 0.002806911838696635. which if computed
5433.001953125 x 0.002806911838696635 = 15.2
But then you use this factor meant for areas unchanged as conversion factor in the X number format array.
The X number format array is the number format array for measurement of change along the x axis and, if Y is not present, along the y axis as well.
Thus, as you don't have a Y entry, your factor for areas must be the conversion factor in X squared!
In other words, use the square root of your area conversion factor as X conversion factor, i.e. replace
float calib = 0.002806911838696635f;
by
float calib = (float)Math.sqrt(0.002806911838696635);
The result now is much better:
But it is not exactly your 15.2 cm². Thus, I checked your numbers, and indeed, your value of 5433.001953125 for the polygone area is slightly off, it should be around 5388.9613.
Using this more correct value I replaced the line above again, this time by:
float calib = (float)Math.sqrt(15.2/5388.96);
and the result:
I'm implementing a little app with Xamarin Forms for a web page, the thing is that in this web is a linear chart with multiple entries and if the user clicks on a point of the line shows info about that point, as you can see in the picture:
Web Line Chart
After some work, I could create a more or less similar line chart using the OxyPlot.Xamarin.Forms plugin with multiple entries which shows the points
My App Line Chart
This is my code:
OnPropertyChanged("GraphModel");
var model = new PlotModel
{
LegendPlacement = LegendPlacement.Outside,
LegendPosition = LegendPosition.BottomCenter,
LegendOrientation = LegendOrientation.Horizontal,
LegendBorderThickness = 0
};
model.PlotType = PlotType.XY;
model.InvalidatePlot(false);
Dictionary<string, List<Prices>> values = HistoricData[Selected.ProductId];
int colorIndex = 0;
List<string> x_names = new List<string>();
foreach (var item in values.Keys)
{
if (item.ToUpper() == Selected.ProductName) { SelectedIndex = colorIndex; }
var lineSeries = new LineSeries()
{
Title = item,
MarkerType = MarkerType.Circle,
};
lineSeries.MarkerResolution = 3;
lineSeries.MarkerFill = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
lineSeries.MarkerStroke = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
lineSeries.MarkerSize = 3;
var points = new List<DataPoint>();
lineSeries.Color = OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
foreach (var price in values[item])
{
points.Add(new DataPoint(price.Week+price.Year, price.Price));
}
if (ButtonsVisibility.Count == 0)
{
lineSeries.IsVisible = (Selected.ProductName == item.ToUpper()) ? true : false;
}
else
{
lineSeries.IsVisible = ButtonsVisibility[colorIndex];
}
lineSeries.ItemsSource = points;
lineSeries.MarkerType = OxyPlot.MarkerType.Circle;
model.Series.Add(lineSeries);
colorIndex++;
}
NumButtons = colorIndex;
LinearAxis yaxis = new LinearAxis();
yaxis.Position = AxisPosition.Left;
yaxis.MajorGridlineStyle = LineStyle.Dot;
model.Axes.Add(yaxis);
LineChart = model;
OnPropertyChanged("GraphModel");
return LineChart;
My doubt is which property I should work with and show at least the value of a concrete point, I have seen the property OnTouchStarted but is only for all the LineSeries and not for a single point. I read in some articles that OxyPlot.Xamarin.Forms include a tracker. I added this line in my code:
lineSeries.TrackerFormatString = "X={2},\nY={4}";
Is supposed to show the x and y values on click but doesn't show anything, any suggestion?
Should show something like that: Tracker info on point
From the following example: Tracker Example
Updated Code
public PlotModel GetLineChart()
{
OnPropertyChanged("GraphModel");
var model = new PlotModel
{
LegendPlacement = LegendPlacement.Outside,
LegendPosition = LegendPosition.BottomCenter,
LegendOrientation = LegendOrientation.Horizontal,
LegendBorderThickness = 0
};
model.PlotType = PlotType.XY;
model.InvalidatePlot(false);
Dictionary<string, List<Prices>> values = HistoricData[Selected.ProductId];
int colorIndex = 0;
List<string> x_names = new List<string>();
foreach (var item in values.Keys)
{
if (item.ToUpper() == Selected.ProductName) { SelectedIndex = colorIndex; }
var lineSeries = new LineSeries()
{
Title = item,
MarkerType = MarkerType.Circle,
CanTrackerInterpolatePoints = false
};
lineSeries.MarkerResolution = 3;
lineSeries.MarkerFill = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
lineSeries.MarkerStroke = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
lineSeries.MarkerSize = 3;
var points = new List<DataPoint>();
lineSeries.Color = OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
foreach (var price in values[item])
{
points.Add(new DataPoint(price.Week+price.Year, price.Price));
}
if (ButtonsVisibility.Count == 0)
{
lineSeries.IsVisible = (Selected.ProductName == item.ToUpper()) ? true : false;
}
else
{
lineSeries.IsVisible = ButtonsVisibility[colorIndex];
}
lineSeries.ItemsSource = points;
lineSeries.MarkerType = OxyPlot.MarkerType.Circle;
lineSeries.TrackerFormatString = "X={2},\nY={4}";
lineSeries.TextColor = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
model.Series.Add(lineSeries);
colorIndex++;
}
NumButtons = colorIndex;
LinearAxis yaxis = new LinearAxis();
yaxis.Position = AxisPosition.Left;
//yaxis.StringFormat = "X={2},\nY={4}";
yaxis.MajorGridlineStyle = LineStyle.Dot;
model.Axes.Add(yaxis);
LineChart = model;
OnPropertyChanged("GraphModel");
return LineChart;
}
}
protected async override void OnAppearing()
{
await _viewModel.LinearViewModel.GetSubCategoryHistoricWeekPrices(App.ViewModel.LoginViewModel.SesionToken, FROM_DATE, TO_DATE);
Plot.Model = _viewModel.LinearViewModel.GetLineChart();
PlotController controller = new PlotController();
controller.UnbindAll();
controller.BindTouchDown(PlotCommands.PointsOnlyTrackTouch);
Plot.Controller = controller;
AddButtons();
}
Xaml Declaration for plot view:
<oxy:PlotView
Grid.Row="2"
Grid.RowSpan="2"
Grid.ColumnSpan="4"
x:Name="Plot" />
Your problem lies with following line.
lineSeries.TrackerKey = "X={2},\nY={4}";
When you use series.TrackerKey, you are specifying that you are using a CustomTracker, which in this case you are not. Custom trackers would be useful if you need to use different trackers for each series in the model.
In you case, you should remove that line and use only TrackerFormatString.
lineSeries.TrackerFormatString = "X={2},\nY={4}";
This would show the tooltip using the format string parameters, where {2} signifies X Value and {4} signifies Y. For your information, following are place holders.
{0} = Title of Series
{1} = Title of X-Axis
{2} = X Value
{3} = Title of Y-Axis
{4} = Y Value
If you need to include additional/custom information in your tool, your Data Point needs to be include that information. This where IDataPointProvider interface becomes handy. You could create a Custom DataPoint by implementing the interface and then you could include the same information in your tooltip as well.
Update Based On Comments
Additionally, to include "Touch", you can specify TouchDown in the PlotController. You can do so by defining the PlotController in your viewModel as following.
public PlotController CustomPlotController{get;set;}
You can define the property as follows.
CustomPlotController = new PlotController();
CustomPlotController.UnbindAll();
CustomPlotController.BindTouchDown(PlotCommands.PointsOnlyTrackTouch);
And in your Xaml
<oxy:Plot Controller="{Binding CustomPlotController}"......
I am using the DynamicControlsPlaceholder by Denis Bauer to save the viewstate of dynamic controls after postback.
I used DynamicControlsPlaceholder before in an earlier part of my project and it worked flawlessly.
However, today I have run into difficulty. I have created a page where there are a number of text labels, slider bars and textboxes (defined by how many elements there are on a database) as shown below. The slider bars are JuiceUI slider controls and the text boxes are normal ASP.NET textboxes.
After postback the text labels (literal controls) and pie chart disappear, the textboxes reduce in size (text inside remains) and the sliderbars are reset to the lowest value without the ability to move the slider (the sliders cannot move at all).
I am quite new to ASP.NET and I am completely stumped as to why this is happening. Do you think it is a problem with the dynamic control placeholder, JuiceUI slider or my code (see below)?
{
SqlCeCommand cmdb = new SqlCeCommand();
cmdb.CommandText = "SELECT CriteriaName,CriteriaDesc FROM tblCriteria WHERE (DecisionID = #DID)";
cmdb.Parameters.AddWithValue("#DID", DID.Text.Trim());
cmdb.Connection = sqlConnection1;
reader = cmdb.ExecuteReader();
string[] criterianames = new string[critno];
string[] criteriadescs = new string[critno];
int i = 0;
while (reader.Read())
{
criterianames[i] = reader["CriteriaName"].ToString().Trim();
criteriadescs[i] = reader["CriteriaDesc"].ToString().Trim();
i++;
}
reader.Close();
Cont2.Controls.Add(new LiteralControl("<h3>Thank you for contributing to the following decision.<h4>Decision Goal: " + dgoal + "</h4><br><br><center>"));
Series weights = new Series();
weights.ChartType = SeriesChartType.Pie;
double[] yBar = new double[critno];
string[] xBar = new string[critno];
xBar = criterianames;
for (i = 0; i < critno; i++)
{
yBar[i] = 1;
}
ChartArea ca = new ChartArea();
ca.Position = new ElementPosition(0, 0, 100, 100);
ca.InnerPlotPosition = new ElementPosition(0, 0, 100, 100);
ca.BackColor = System.Drawing.Color.Transparent;
Chart piechart = new Chart();
piechart.RenderType = RenderType.ImageTag;
piechart.ChartAreas.Add(ca);
piechart.BackColor = System.Drawing.Color.Transparent;
piechart.Palette= ChartColorPalette.BrightPastel;
piechart.BorderColor = System.Drawing.Color.Black;
piechart.BorderSkin.PageColor = System.Drawing.Color.Transparent;
piechart.BorderSkin.BackColor = System.Drawing.Color.Transparent;
piechart.Width = 800;
piechart.Series.Add(weights);
piechart.ImageStorageMode = ImageStorageMode.UseImageLocation;
piechart.ImageLocation = "~/TempImages/ChartPic_#SEQ(300,3)";
piechart.Series[0].Points.DataBindXY(xBar, yBar);
piechart.DataBind();
Cont2.Controls.Add(piechart);
Cont2.Controls.Add(new LiteralControl("</center><h3>Please provide a weighting for each criterion.</h3><p>Please provide a weighting for each criterion along with a description of why you made this choice. </p>"));
for (i = 0; i < critno; i++)
{
Cont3.Controls.Add(new LiteralControl("<h3>" + criterianames[i] + "</h3><p><strong>Description: </strong>" + criteriadescs[i] + "</p><center>"));
Juice.Slider weightslider = new Juice.Slider();
weightslider.ID = "w" + i.ToString();
weightslider.Min = 1;
weightslider.Value = 50;
weightslider.Max = 100;
weightslider.AutoPostBack = true;
Cont3.Controls.Add(weightslider);
weightslider.ValueChanged += (o, a) =>
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + weightslider.Value.ToString() + "');", true);
};
TextBox wdesc = new TextBox();
wdesc.ID = "wd" + Convert.ToString(i);
wdesc.Rows = 3;
wdesc.Width = 900;
wdesc.TextMode = TextBoxMode.MultiLine;
Cont3.Controls.Add(wdesc);
Cont3.Controls.Add(new LiteralControl("</center>"));
}
Cont3.Controls.Add(new LiteralControl("<p align='right'>"));
Button continue1 = new Button();
continue1.Text = "Continue";
Cont3.Controls.Add(continue1);
Cont3.Controls.Add(new LiteralControl("</p>"));
// Database Disconnect
sqlConnection1.Close();
}
Many thanks for any help you can provide,
Kind regards,
Richard
You could eliminate or confirm the problem exists with Juice UI by creating a page containing nothing more than a Juice UI slider, one of these dynamic placeholders and a label. That'd be the first stop.
If you do run into problems with Juice UI, you can use it's cousin Brew
I have a radchart that is supposed to display two lines with separate Y values, but when it loads it displays two lines, but they both have Y values that they are supposed to display and the Y values of the other line in the chart
ie:
(Data set 1: 1,2,6,9)
(Data set 2: 1,4,6,7)
(Lines display like: 1-1-2-4-6-6-9-7 and 1-1-2-4-6-6-9-7)
my series mapping looks like this:
<telerik:SeriesMapping LegendLabel="Line Series 1" >
<telerik:SeriesMapping.SeriesDefinition>
<telerik:StackedLineSeriesDefinition />
</telerik:SeriesMapping.SeriesDefinition>
<telerik:ItemMapping FieldName="LineYValue" DataPointMember="YValue" />
</telerik:SeriesMapping>
<telerik:SeriesMapping LegendLabel="Line Series 2" >
<telerik:SeriesMapping.SeriesDefinition>
<telerik:StackedLineSeriesDefinition />
</telerik:SeriesMapping.SeriesDefinition>
<telerik:ItemMapping FieldName="LineYValue" DataPointMember="YValue" />
</telerik:SeriesMapping>
</telerik:RadChart.SeriesMappings>
and in my ViewModel I have the data loaded like so
if (salesAndQtyByHour != null)
{
STUFF.ChartModels.Charts tempChart1 = new ODA.ChartModels.Charts() { ChartTitle = "Sales And Quantity By Hour" };
foreach (var item in salesAndQtyByHour)
{
tempChart1.myCharts.Add(new STUFF.ChartModels.Chart() { LineName = "Quantity", LineXValue = item.MilitaryTime, LineYValue = item.QtyOfItems });
tempChart1.myCharts.Add(new STUFF.ChartModels.Chart() { LineName = "Basket", LineXValue = item.MilitaryTime, LineYValue = item.Basket });
}
SalesAndQtyByHour = tempChart1;
}
I'm pretty sure its the CollectionIndex, but I'm not sure how to implement it in my case, I'm relatively new to this job.
Any help would be appreciated.
There is ItemsSource property of the SeriesMapping object. You can send each individual dataset to the corresponding SeriesMapping. Article:
http://www.telerik.com/help/silverlight/radchart-populating-with-data-series-mapping-items-source.html