How to change OxyPlot Y-Axis string format? - c#

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.

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.

Oxyplot Polar Diagram with Categories

I'd like to create a polar diagram with oxyplot. The circular axis should not consist of integers, but of categories.
Meaning instead of 1 ... 10 it should say Category A Category B ... around the plot.
Neither MagnitudeAxis nor AngularAxis provide the possibility to set "strings" for the axis.
CategoryAxis however cannot be used to plot a polar diagram, because it does not support angles.
My code so far:
var plotModel = new PlotModel { Title = "", };
plotModel.PlotType = OxyPlot.PlotType.Polar;
plotModel.Axes.Add(new OxyPlot.Axes.AngleAxis()
{
MajorGridlineStyle = LineStyle.Solid,
//MinorGridlineStyle = LineStyle.Dot,
MajorStep = 1,
CropGridlines = false,
StartAngle = 450,
EndAngle = 90,
Minimum = 0,
Maximum = 19
});
plotModel.Axes.Add(new OxyPlot.Axes.MagnitudeAxis()
{
MajorGridlineStyle = LineStyle.Solid,
Minimum = 0,
Maximum = 5,
MajorStep = 1,
MinorStep = 1
});
var newValues = new OxyPlot.Series.LineSeries { Title = "New Values", StrokeThickness = 1 };
int i = 0;
foreach(var dataRow in details)
{
newValues.Points.Add(new DataPoint(dataRow.NewValue, i++)); //instead of i++ I would like to put a string of the object dataRow, but this is not supported...
}
In lack of examples and documentation online, this is my last hope to find some help...
It looks to me like the LabelFormatter property is what you need. The code below creates labels 'Category A', 'Category B'...'Category S' around the outside of the plot. It does this because (char)65 is 'A', (char)66 is 'B' etc.
plotModel.Axes.Add(new OxyPlot.Axes.AngleAxis()
{
MajorGridlineStyle = LineStyle.Solid,
MajorStep = 1,
CropGridlines = false,
StartAngle = 450,
EndAngle = 90,
Minimum = 0,
Maximum = 19,
LabelFormatter = d => $"Category {(char)(d+65)}"
});
Based on the answer of Rich N I was able to find a (nasty) workaround by myself:
plotModel.Axes.Add(new OxyPlot.Axes.AngleAxis()
{
MajorGridlineStyle = LineStyle.Solid,
//MinorGridlineStyle = LineStyle.Dot,
MajorStep = 1,
CropGridlines = false,
StartAngle = 450,
EndAngle = 90,
Minimum = 0,
Maximum = 19,
LabelFormatter = d => myCategoryList[Convert.ToInt32(d)] //myCategoryList is a list of strings
});

c# windows application Live Charts x axis date forma

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

OxyPlot: how to hide left and top axis lines

I'm using to Oxyplot for my Xamarin.iOS project for plotting a bar chart
This is what my plot currently looks like
Here I need to hide the Right and top axis
I tried :
model.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Right,
IsAxisVisible = false
});
model.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Top,
IsAxisVisible = false
});
But no effect.. Here's my full code
public MyClass()
{
var model = new PlotModel { Title = "ColumnSeries" };
// A ColumnSeries requires a CategoryAxis on the x-axis.
model.Axes.Add(new CategoryAxis()
{
Position = AxisPosition.Bottom,
MinorTickSize = 0,
//MajorGridlineStyle = LineStyle.Solid,
MinorGridlineStyle = LineStyle.Solid,
});
model.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Left,
MinorTickSize = 0,
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineStyle = LineStyle.Solid,
Minimum = 0,
Maximum = 400
});
model.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Right,
IsAxisVisible = false
});
model.Axes.Add(new LinearAxis()
{
Position = AxisPosition.Top,
IsAxisVisible = false
});
var series = new ColumnSeries();
series.Items.Add(new ColumnItem() { Value = 200});
series.Items.Add(new ColumnItem(200));
series.Items.Add(new ColumnItem(300));
series.Items.Add(new ColumnItem(100));
series.Items.Add(new ColumnItem(200));
series.Items.Add(new ColumnItem(100));
series.Items.Add(new ColumnItem(130));
model.Series.Add(series);
this.MyModel = model;
}
How do I do it? Any help is appreciated....
Edit:
Also, In my chart above, why are the y labels not showing. How can I change the x labels like below... Is it possible to draw lines in this chart like below?
This is what I want my final graph to look like:
The problem is that the black border you are currently seeing is not the axis, is the plot area border, so you have to modify this property in the plotmodel:
model.PlotAreaBorderColor = OxyColors.Transparent;
And then, you have to add the AxisLineStyle to the axis that you want to draw(left and bottom), like this:
model.Axes.Add(new LinearAxis()
{
AxislineStyle = LineStyle.Solid,
Position = AxisPosition.Left,
MinorTickSize = 0,
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineStyle = LineStyle.Solid,
Minimum = 0,
Maximum = 400
});

How can I avoid that ZedGraph relabels my YAxis, dividing by 1000?

I am creating a C# visual studio forms application that uses zedgraph to chart the data that the program collects but I am running into the following issue when plotting the data:
My y-axis values are usually in the 100,000+ range so when zed graph plots the value it labels the y-axis labels with stuff like 0, 10, 15, 20, 25 and then on the y-axis label it will append "(10^3)" to the title and will plot the values accordingly. What I want to do is have it label the y-axis either with values like 0, 10,000, 15,000, 20,000 etc or 0, 10k, 15k, 20k and so on and not have it adjust the y-axis title.
I tried setting YAxis.Scale.MajorStep = double.Parse("10000"); but the only effect that has is to add a ton of more tick lines on the y-axis but no other effect. Here is my code that graphs the data:
private void createGraph()
{
GraphPane myPane = zdc_graph.GraphPane;
myPane.CurveList.Clear();
myPane.GraphObjList.Clear();
myPane.Title.Text = this.monitoredHost.hostName + "\nWorkState[" +
this.monitoredHost.currentWorkState + "]";
myPane.XAxis.Title.Text = "";
myPane.YAxis.Title.Text = "OPS Per Second";
myPane.YAxis.Scale.FontSpec.FontColor = Color.Blue;
myPane.YAxis.Title.FontSpec.FontColor = Color.Blue;
myPane.YAxis.Scale.MaxAuto = true;
myPane.Y2Axis.Title.Text = "Reading";
myPane.Y2Axis.IsVisible = true;
myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Red;
myPane.Y2Axis.Title.FontSpec.FontColor = Color.Red;
myPane.XAxis.Type = AxisType.Date;
myPane.XAxis.Scale.Format = "T";
myPane.XAxis.Scale.MajorUnit = DateUnit.Second;
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.MajorStep = double.Parse("10000");
myPane.Y2Axis.Scale.Min = 0;
LineItem kpiCurve = myPane.AddCurve("OPS Per Second",
this.monitoredHost.graphKpiList,
Color.Blue,SymbolType.Circle);
LineItem pwrCurve = myPane.AddCurve("Reading",
this.monitoredHost.graphPwrList, Color.Red,
SymbolType.Circle);
kpiCurve.Line.Width = 2.0F;
kpiCurve.Symbol.Size = 4.0F;
kpiCurve.Symbol.Fill = new Fill(Color.White);
pwrCurve.Line.Width = 2.0F;
pwrCurve.Symbol.Size = 4.0F;
pwrCurve.Symbol.Fill = new Fill(Color.White);
pwrCurve.IsY2Axis = true;
myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 210), -45F);
zdc_graph.AxisChange();
zdc_graph.Refresh();
}
I hope this makes sense. Thanks for the help.
ZedGraph is attempting to detect magnitude and simplify the graph. You can turn this off with the following:
myPane.YAxis.Scale.MagAuto = false;
This will result in y-axis labels like 100000.
If you want to format the label with a separator comma like 100,000:
myPane.YAxis.Scale.Format = "#,#";
Finally, if you prefer to show 100k, you'll need to subscribe to the ScaleFormatEvent and return your own format, like this:
myPane.YAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(YAxis_ScaleFormatEvent);
string YAxis_ScaleFormatEvent(GraphPane pane, Axis axis, double val, int index)
{
return String.Format("{0}k", val / 1000);
}
I am having similar problem. So applying your method it works on the application but i also want to print out the graph in a PDF file (using MigraDoc) but it does work.
public Bitmap printGraphPane()
{
ZedGraphControl graph = new ZedGraphControl();
GraphPane newGP = myPane.GraphPane;
//newGP.YAxis.Scale.Mag = 0;
//newGP.YAxis.Scale.Format = "#";
//newGP.YAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(YAxis_ScaleFormatEvent);
Bitmap bit = new Bitmap(newGraph.Width, newGraph.Height);
newGraph.ClientSize = bit.Size;
newGraph.DrawToBitmap(bit, new Rectangle(0, 0, newGraph.Width, newGraph.Height));
return bit;
}

Categories

Resources