c# windows application Live Charts x axis date forma - c#

Hello everyone i working in project that convert data (date and value) to graphic curve .
i have problem with x axis the value of date printing in double format , i want this values showing like this format 14:12:35
var gradientBrush = new LinearGradientBrush
{
StartPoint = new System.Windows.Point(0, 0),
EndPoint = new System.Windows.Point(0, 1)
};
gradientBrush.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromRgb(33, 148, 241), 0.2));
gradientBrush.GradientStops.Add(new GradientStop(Colors.Transparent, 1));
cartesianChart1.Series.Add(new LineSeries
{
Values = GetData(),
Fill = gradientBrush,
StrokeThickness = 0.9,
PointGeometry = null
});
cartesianChart1.Zoom = ZoomingOptions.X;
private ChartValues<DateTimePoint> GetData()
{
var values = new ChartValues<DateTimePoint>();
for (var i = 0; i <lsTemp.Count(); i++)
{
// System.DateTime.Today.AddDays(i)
values.Add(new DateTimePoint(lsDataTime[i], lsTemp[i]));
}
return values;
}
enter image description here

you need to set LabelFormatter property of Axis class.
Something like this:
AxisX = new Axis
{
Title = "Date",
Separator = new Separator { IsEnabled = false,Step = 1 },
LabelsRotation = -90,
Foreground = new SolidColorBrush(Colors.Black),
LabelFormatter = value => new System.DateTime((long)value).ToString("t")
};
And look at this link for formats ToString data formats

Related

Why is my reversed MagnitudeAxis Polar Plot not rendering with Oxyplot?

I'm trying to create a Polar Plot with Oxyplot in my WPF application. I want the MagnitudeAxis to be reversed so I made the StartPosition = 1 and the EndPosition = 0. However this causes the plot not to render. It looks it may be due to the variables a0 and a1 being set to 0 in the UpdateTransform() method in MagnitudeAxis.cs but it may be something else (not exactly sure what all these variables represent).
Here's my code to set up the polar plot:
/// dictionary of angles: magnitudes
dict = {-180: -18, -179: -17.9, ... , 0: 0, ... , 178: -17.8, 179: -17.9}
PlotModel myPlot = new PlotModel
{
Title = "Polar Plot",
PlotType = PlotType.Polar,
PlotAreaBorderThickness = new OxyThickness(0),
};
var series1 = new ScatterSeries
{
Title = "Polar Series",
MarkerType = MarkerType.Circle,
MarkerSize = 3,
MarkerFill = OxyColor.FromRgb(255, 0, 0),
};
var axis1 = new AngleAxis
{
StartPosition = 1,
EndPosition = 0,
StartAngle = 270,
EndAngle = -90,
Title = "Angle",
LabelFormatter = d => $"{d}",
MajorStep = 30,
Minimum = -180,
Maximum = 180,
MinorGridlineStyle = LineStyle.None,
};
var axis2 = new MagnitudeAxis
{
/// here's where I set the Start and End Position
StartPosition = 1,
EndPosition = 0,
MajorStep = 6,
Title = "Magnitude",
MinorGridlineStyle = LineStyle.None,
Minimum = dict.Values.Min();
Maximum = dict.Values.Max()
};
foreach (KeyValuePair<double, double> keyValuePair in dict)
{
series1.Points.Add(new ScatterPoint(keyValuePair.Value, keyValuePair.Key));
}
myPlot.Axes.Add(axis1);
myPlot.Axes.Add(axis2);
myPlot.Series.Add(series1);
This is what I get when the plot tries rendering:
This is what I get when I use default Start/End Positions:
What I want is the default plot with 0 in the center and -18 on the edge instead.
This is my first question ever on SO, so let me know if there's any other information I should provide.

ZedGraph How To Create a Classic Line Chart With XAxis.Type = AxisType.Text

I'm trying to create a Line Graph with zed graph. I just wanted to ask that how can I create a Line Graph which XAxis type is text and YAxis type is doubles.
Firstly i was really searching about this topic but i didnt get any result about it. Because other Line graphs are always about date&time on XAxis. I don't need date&time on XAxis. I will use labels for XAxis to name points on YAxis .
Here an Example Graph
string[] labels = { "P(A)", "P(A)+P(T)", "P(A)+P(T)+P(G)", "P(A)+P(T)+P(G)+P(C)" };
double[] y = { PA(), PA() + PT(), PA() + PT() + PG(), PA() + PT() + PG() + PC() };
LineItem myLine = myPane.AddCurve("dizi 1", null, y, Color.Red);
myLine.Line.Fill = new Fill(Color.Red, Color.White, Color.Red);
myPane.XAxis.Scale.TextLabels = labels;
myPane.XAxis.Type = AxisType.Text;
myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
zedGraphControl1.AxisChange();
****Codes are above . Is there anything wrong ?****
I just figured it out!
Here is example codes to create a basic line graph!
private void button3_Click(object sender, EventArgs e)
{
// generate some fake data
double[] y = { 1, 2, 3, 9 ,1,15,3,7,2};
string[] schools = { "A", "B", "C", "D" ,"E","F","G","H","J"};
//generate pane
var pane = zg1.GraphPane;
pane.XAxis.Scale.IsVisible = true;
pane.YAxis.Scale.IsVisible = true;
pane.XAxis.MajorGrid.IsVisible = true;
pane.YAxis.MajorGrid.IsVisible = true;
pane.XAxis.Scale.TextLabels = schools;
pane.XAxis.Type = AxisType.Text;
//var pointsCurve;
LineItem pointsCurve = pane.AddCurve("", null, y, Color.Black);
pointsCurve.Line.IsVisible = true;
pointsCurve.Line.Width = 3.0F;
//Create your own scale of colors.
pointsCurve.Symbol.Fill = new Fill(new Color[] { Color.Blue, Color.Green, Color.Red });
pointsCurve.Symbol.Fill.Type = FillType.Solid;
pointsCurve.Symbol.Type = SymbolType.Circle;
pointsCurve.Symbol.Border.IsVisible = true;
pane.AxisChange();
zg1.Refresh();
this.Controls.Add(zg1);
}

How to change OxyPlot Y-Axis string format?

Can anyone tell me how to change the Y axis string format??
I have Y-Axis percentages that I want to add the percent sign to.
I am using OxyPlot to produce the chart in wpf.
Here is my attempt, but it is NOT working:
Func<double, string> formatFunc = (x) => string.Format("{000.00}%", x);
formatFunc = new Func<double,string>("{0}");
// Add the plot to the window
line.YAxis.LabelFormatter = formatFunc;
This produces null reference error.
Thanks!
This is an example I've used previously to format the x-axis on an oxy-plot:
var xAxis = new DateTimeAxis
{
Position = AxisPosition.Bottom,
StringFormat = "dd/MM/yyyy",
Title = "End of Day",
IntervalLength = 75,
MinorIntervalType = DateTimeIntervalType.Days,
IntervalType = DateTimeIntervalType.Days,
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineStyle = LineStyle.None,
};
Plot = new PlotModel();
Plot.Axes.Add(xAxis);
this._PlotModel.Axes.Add( new LinearAxis
{
Position = AxisPosition.Left,
Minimum = 0.025,
Maximum = 0.205,
StringFormat = "0.00%",
MajorStep = 0.005
} );
- add percents as ratio: 20.5% --> 0.205, 0.5% --> 0.005, etc.

How do I make a pie-in-pie chart with MSChart?

I've downloaded MS Chart samples and in the gallery it shows a "3DPieInPie.png", which is what I want - but can't see any code to create it!!
Presumably it's ChartType = SeriesChartType.Pie ... but is there another setting that specifies I want the second pie? And how do enter the second set of data? - I've tried adding a new series, but it seems to be ignored.
This type of diagram is called nested pie chart, or multilevel pie chart.
It is done by creating two ChartArea - pie chart and donut chart,
which are correctly placed one relative to the other.Here is code below.
public partial class Form1 : Form
{
public class DataInt {
public string Label { get; set; }
public int Value { get; set; } }
public Form1()
{
InitializeComponent();
pieChart.Series.Clear();
pieChart.Legends.Clear();
float baseDoughnutWidth = 25;
float outerSize = 80;
ChartArea outerArea = new ChartArea("OUTER_AREA");
outerArea.Position = new ElementPosition(0, 10, 100, 100);
outerArea.InnerPlotPosition = new ElementPosition((100 - outerSize) / 2, (100 - outerSize) / 2, outerSize, outerSize);
outerArea.BackColor = Color.Transparent;
pieChart.ChartAreas.Add(outerArea);
float innerSize = 53;
ChartArea innerArea = new ChartArea("INNER_AREA");
innerArea.Position = new ElementPosition(0, 5, 100, 100);
ElementPosition innerPos = new ElementPosition((100 - innerSize) / 2, ((100 - innerSize) / 2), innerSize, innerSize+5);
innerArea.InnerPlotPosition = innerPos;
innerArea.BackColor = Color.Transparent;
pieChart.ChartAreas.Add(innerArea);
Series outerSeries = new Series("OUTER_SERIES");
outerSeries.ChartArea = "OUTER_AREA";
outerSeries.ChartType = SeriesChartType.Doughnut;
outerSeries["DoughnutRadius"] = Math.Min(baseDoughnutWidth * (100 / outerSize), 99).ToString().Replace(",", ".");
Series innerSeries = new Series("INNER_SERIES");
innerSeries.ChartArea = "INNER_AREA";
innerSeries.ChartType = SeriesChartType.Pie;
var innerData = new List<DataInt> { new DataInt { Label = "A", Value = 7 },
new DataInt { Label = "B", Value = 14 }, new DataInt{Label="C",Value=19},
new DataInt{Label="D",Value=12}, new DataInt{Label="E",Value=20},
new DataInt{Label="F",Value=28} };
var outerData = new List<DataInt> { new DataInt { Label = "Gold", Value = 21 },
new DataInt{Label="Red",Value=31}, new DataInt { Label = "Blue", Value = 48 } };
innerSeries.Points.DataBindXY(innerData, "Label", innerData, "Value");
outerSeries.Points.DataBindXY(outerData, "Label", outerData, "Value");
pieChart.Series.Add(innerSeries);
pieChart.Series.Add(outerSeries);
Legend legend = new Legend("pieChartLegend")
{
Font=new System.Drawing.Font("Arial",14.0f),
Alignment = StringAlignment.Center,
Docking = Docking.Top,
Enabled = true,
IsDockedInsideChartArea = false,
TableStyle = LegendTableStyle.Wide,
};
pieChart.Legends.Add(legend);
foreach (Series sr in pieChart.Series)
{
sr.Legend = "pieChartLegend";
}
var outerAreaColors = new List<Color>(){Color.Gold,Color.Red, Color.DeepSkyBlue };
var innerAreaColors = new List<Color>() {Color.Goldenrod,Color.DarkGoldenrod, Color.DarkRed, Color.Firebrick,Color.DodgerBlue, Color.SteelBlue };
for (int i = 0; i < outerSeries.Points.Count; i++)
{
outerSeries.Points[i].Color = outerAreaColors[i];
}
for (int i = 0; i < innerSeries.Points.Count; i++)
{
innerSeries.Points[i].Color = ChartColorPallets.Inner[i];
}
int inclination = 20;
int rotation = 45;
bool enable3d = true;
pieChart.ChartAreas["OUTER_AREA"].Area3DStyle.Enable3D = enable3d;
pieChart.ChartAreas["OUTER_AREA"].Area3DStyle.Inclination = inclination;
pieChart.ChartAreas["OUTER_AREA"].Area3DStyle.Rotation = rotation;
pieChart.ChartAreas["INNER_AREA"].Area3DStyle.Enable3D = enable3d;
pieChart.ChartAreas["INNER_AREA"].Area3DStyle.Inclination = inclination;
pieChart.ChartAreas["INNER_AREA"].Area3DStyle.Rotation = rotation;
}
}
Check out these answers. They also include some sample code:
http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/54b4dbb7-1622-4798-8b6a-ef4f01e48c31/
http://support2.dundas.com/default.aspx?article=1115
http://support2.dundas.com/Default.aspx?article=1114

Issues regarding image and random row color for a table in openxml?

I'm using an HTML to OpenXML convertor, gridview HTML code is assigned to a stringbuilder. Using that we convert that HTML to OpenXML, but when it comes to word the following below issues are found.
I have to fill background of a row of a table with grainsboro color but only text background is filled and not cell completely.
I wanted to align the image of the header to the right.
This is an asp.net application done in C#
A***building image code plz tell me how to align the image to right***
d=DocumentFormat.OpenXml.Drawing;
int emuWidth = (int)(pixelWidth * EMU_PER_PIXEL);
int emuHeight = (int)(pixelHeight * EMU_PER_PIXEL);
Drawing drawing = new Drawing();
d.Wordprocessing.Inline inline = new d.Wordprocessing.Inline { DistanceFromTop = 130, DistanceFromBottom = 430, DistanceFromLeft = 260, DistanceFromRight = 330 };
d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 };
d.Wordprocessing.SimplePosition simplePos = new d.Wordprocessing.SimplePosition { X = 0, Y = 0 };
d.Wordprocessing.Extent extent = new d.Wordprocessing.Extent { Cx = emuWidth, Cy = emuHeight };
d.Wordprocessing.DocProperties docPr = new d.Wordprocessing.DocProperties { Id = 1, Name = imageName };
d.Wordprocessing.HorizontalPosition h = new d.Wordprocessing.HorizontalPosition(new d.Wordprocessing.HorizontalAlignment("right"));
d.Graphic graphic = new d.Graphic();
// We don’t have to hard code a URI anywhere else in the document but if we don’t do it here
// we end up with a corrupt document.
d.GraphicData graphicData = new d.GraphicData { Uri = GRAPHIC_DATA_URI };
d.Pictures.Picture pic = new d.Pictures.Picture();
d.Pictures.NonVisualPictureProperties nvPicPr = new d.Pictures.NonVisualPictureProperties();
d.Pictures.NonVisualDrawingProperties cNvPr = new d.Pictures.NonVisualDrawingProperties { Id = 2, Name = imageName };
d.Pictures.NonVisualPictureDrawingProperties cNvPicPr = new d.Pictures.NonVisualPictureDrawingProperties();
d.Pictures.BlipFill blipFill = new d.Pictures.BlipFill();
d.Blip blip = new d.Blip { Embed = imageRelationshipID };
d.Stretch stretch = new d.Stretch();
d.FillRectangle fillRect = new d.FillRectangle();
d.Pictures.ShapeProperties spPr = new d.Pictures.ShapeProperties();
d.Transform2D xfrm = new d.Transform2D();
d.Offset off = new d.Offset { X = 0, Y = 0 };
d.Extents ext = new d.Extents { Cx = emuWidth, Cy = emuHeight };
d.PresetGeometry prstGeom = new d.PresetGeometry { Preset = d.ShapeTypeValues.Rectangle };
d.AdjustValueList avLst = new d.AdjustValueList();
xfrm.Append(off);
xfrm.Append(ext);
prstGeom.Append(avLst);
stretch.Append(fillRect);
spPr.Append(xfrm);
spPr.Append(prstGeom);
blipFill.Append(blip);
blipFill.Append(stretch);
nvPicPr.Append(cNvPr);
nvPicPr.Append(cNvPicPr);
pic.Append(nvPicPr);
pic.Append(blipFill);
pic.Append(spPr);
graphicData.Append(pic);
graphic.Append(graphicData);
inline.Append(extent);
inline.Append(docPr);
inline.Append(graphic);
//anchor.Append(extent);
//anchor.Append(docPr);
//anchor.Append(h);
//anchor.Append(graphic);
drawing.Append(inline);
return drawing;
I think in OpenXML there is a property highlight text color. How do I get that in C# code?
Actually you don't set the alignment of drawing directly. You place it in paragraph for example and set its property alignment.
this is the code I'm using:
public void AddImageParagraph(Drawing element, JustificationValues alignment = JustificationValues.Left)
{
Paragraph paragraph = new Paragraph();
ParagraphProperties paragraphProperties = new ParagraphProperties();
Justification justification = new Justification()
{
Val = alignment
};
paragraphProperties.Append(justification);
Run run = new Run(element);
paragraph.Append(run);
paragraph.Append(paragraphProperties);
mBody.Append(paragraph);
}

Categories

Resources