Add grid lines to y-axis in NPOI bar chart - c#

I'd like to add horizontal grid lines to a bar chart (IChart) I created using NPOI. I can't find anything in the documentation about adding grid lines to a chart, and I'm wondering if I'm missing something. My code is shown below:
IDrawing drawing = sheet.CreateDrawingPatriarch();
IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 4, chartRow, 10, chartRow + 30);
IChart chart = drawing.CreateChart(anchor);
IBarChartData<string, double> data = chart.ChartDataFactory.CreateBarChartData<string, double>();
IChartAxis xAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
xAxis.MajorTickMark = AxisTickMark.None;
xAxis.MinorTickMark = AxisTickMark.None;
IValueAxis yAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
yAxis.MajorTickMark = AxisTickMark.Cross;
yAxis.MinorTickMark = AxisTickMark.None;
yAxis.Crosses = AxisCrosses.AutoZero;
yAxis.SetCrossBetween(AxisCrossBetween.Between);
IChartDataSource<string> xSource = DataSources.FromStringCellRange(sheet, new CellRangeAddress(chartRow + 1, lastRow, 0, 0));
IChartDataSource<double> ySource = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(chartRow + 1, lastRow, 1, 1));
IBarChartSeries<string, double> series = data.AddSeries(xSource, ySource);
series.SetTitle("Matches By World-Check Category"); // This doesn't display for some reason
chart.Plot(data, xAxis, yAxis);

Displaying minor and major gridlines for chart is not supported.
I would suggest to create a ticket for an enhancement.

You can add the gridlines with something like this
chart.GetCTChart().plotArea.valAx[0].AddNewMajorGridlines(); // Alongside Y
chart.GetCTChart().plotArea.catAx[0].AddNewMajorGridlines(); // Alongside X

Related

Custom chart axis, ticks and grid in Winforms

I'm developping a WinForms app in C# for plotting multiple lines on a logarithmic scale. I'm trying to display my data on a grid that would look like this:
Logarithmic grid, with major and minor ticks at specified positions.
The data I'm plotting matches specific values on the X-Axis (1, 2, 4, 8 and 16) but there's an offset on the axis (the origin is lower than 1 and the max value is greater than 16) because I have to plot several lines with error bars (I need some extra width).
I'm struggling to find a way to specify exact positions for where my major/minor ticks to be placed.
The closer I got was by specifying chart.ChartAreas[0].AxisX.LogarithBase = 2 and chart.ChartAreas[0].AxisY.LogarithBase = 2, but since my origin is not set at (1,1) I get this as a result:
The closer I got to the grid I'm looking for.
Ideally, I'd like to have a collection of values that specifies the position of the ticks. How one could achieve that? I feel like I'm getting closer by using the CustomLabel class but I'm not in there yet.
Thanks!
I tried to add CustomLabels for specifying the ticks position but couldn't find a way to have the ticks placed at the position I specified: I only found a way to put ticks at specified "indexes" that I struggle to have placed at the correct positions.
I tried playing with the axis intervals too, with no success either.
Maybe not the answer you are looking for so don't worry about accepting, but for you or anyone who happens to read this. Using a 3rd party tool will make this easy, plus you can ask questions directly to the authors of the chart. Full disclosure, I own Gigasoft.
Instead of trying to force your chart to produce the grid in your current approach, instead, maybe your charting api has a feature to draw random annotations, lines, and text, and construct your own axes/grid via these lines and text.
For example, with Gigasoft ProEssentials, the x axis could be created as follows, the y axis would be similar. This is based on our example 110 in our demo.
Pesgo1.PeGrid.Option.LogScaleExpLabels = false; // optional
Pesgo1.PeAnnotation.Line.TextSize = 100; // to match size of normal grid text
Pesgo1.PeGrid.Option.ShowXAxis = ShowAxis.NoNumbersOrLabels; // hide default grid line labels
Pesgo1.PeAnnotation.Line.BottomMargin = "X"; // used with below to add space of line annotation text outside grid
Pesgo1.PeAnnotation.Line.ShowMargins = ShowMargins.Always; // adds space for line annotation text
int n = 0;
// add grid lines with grid number labels
Pesgo1.PeAnnotation.Line.XAxis[n] = 1.0;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridLine;
Pesgo1.PeAnnotation.Line.XAxisText[n] = "|h1";
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = 2.0;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridLine;
Pesgo1.PeAnnotation.Line.XAxisText[n] ="|h2";
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = 4.0;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridLine;
Pesgo1.PeAnnotation.Line.XAxisText[n] = "|h4";
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = 8.0;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridLine;
Pesgo1.PeAnnotation.Line.XAxisText[n] = "|h8";
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = 16.0;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridLine;
Pesgo1.PeAnnotation.Line.XAxisText[n] = "|h16";
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
n++;
// add extra grid lines 11 - 15
for (int x = 11; x <= 15; x++)
{
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = x;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.Dot;
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(80, 200, 200, 200);
Pesgo1.PeAnnotation.Line.XAxisText[n] = " ";
}
// add extra ticks 1 to 16
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = 3.0;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridTick;
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
Pesgo1.PeAnnotation.Line.XAxisText[n] = " ";
for (int x = 5; x <= 7; x++)
{
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = x;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridTick;
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
Pesgo1.PeAnnotation.Line.XAxisText[n] = " ";
}
for (int x = 9; x <= 15; x++)
{
n++;
Pesgo1.PeAnnotation.Line.XAxis[n] = x;
Pesgo1.PeAnnotation.Line.XAxisType[n] = LineAnnotationType.GridTick;
Pesgo1.PeAnnotation.Line.XAxisColor[n] = Color.FromArgb(200, 200, 200, 200);
Pesgo1.PeAnnotation.Line.XAxisText[n] = " ";
}
// Set the x axis range
Pesgo1.PeGrid.Configure.ManualScaleControlX = ManualScaleControl.MinMax;
Pesgo1.PeGrid.Configure.ManualMinX = .9;
Pesgo1.PeGrid.Configure.ManualMaxX = 18;
Pesgo1.PeAnnotation.Show = true; // show the line annotations
And the resulting image is ...

UICollectionview - removing right padding/margin?

I am trying to do a slideshow display of items and I have an issue where there is a mysterious padding added to the first cell(on the left side) and last cell(right side) as shown below and demonstrated by the black box
DataSource datasource = new DataSource(Vcollection, PageControl, this);
Vcollection.Source = datasource;
doRefreshList = true;
Vcollection.CollectionViewLayout = new UICollectionViewFlowLayout()
{
ItemSize = Vcollection.Frame.Size,
HeaderReferenceSize = new CGSize(0, 0),
SectionInset = UIEdgeInsets.Zero,
ScrollDirection = UICollectionViewScrollDirection.Horizontal,
MinimumInteritemSpacing = 0f, // minimum spacing between cells
MinimumLineSpacing = 0f,
SectionInsetReference = UICollectionViewFlowLayoutSectionInsetReference.ContentInset,
};
Vcollection.ContentInset = UIEdgeInsets.Zero;
Vcollection.ScrollIndicatorInsets = UIEdgeInsets.Zero;
Vcollection.ContentOffset = new CGPoint(0f, 0f);
this.AutomaticallyAdjustsScrollViewInsets = false;
This is my code where I tried to rectify the issue, but to no avail. How can I remove this padding?
------------------EDIT ---------------------
Replacing the last 4 lines with
VehicleCollectionView.ContentInset = new UIEdgeInsets(0, -20, 0, 0);
VehicleCollectionView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Never;
Works, however pagination is still off. attempting to scroll through the collectionview shows huge offsets as shown below where pagination is stuck between 2 items
In your ViewDidLoad() method you can try two write this code
collectionView.ContentInset = new UIEdgeInsets (10, 10, 10, 10);
//collectionView.SectionInset = new UIEdgeInsets (20, 20, 20, 20)

Asp.net chart, how can I set the X axis label position to left aligned instead of centered?

I've spent hours trying to solve this silly problem. I create an histogram with asp chart control. All I want to do is have the xaxis label on the left of the column instead of centered on it. Xaxis lable doesn't seem to have a position property like series do, so I can't figure it out and it's frustrating.
Here's a sample code of the type of graphic I'm talking about to show you what I get approximately:
private void Graphique()
{
// Creating the series
Series series2 = new Series("Series2");
// Setting the Chart Types
series2.ChartType = SeriesChartType.Column;
// Adding some points
series2.Points.AddXY(1492, 12);
series2.Points.AddXY(2984, 0);
series2.Points.AddXY(4476, 1);
series2.Points.AddXY(5968, 2);
series2.Points.AddXY(7460, 2);
series2.Points.AddXY(8952, 12);
series2.Points.AddXY(10444, 4);
series2.Points.AddXY(11936, 3);
series2.Points.AddXY(13428, 3);
series2.Points.AddXY(14920, 5);
series2.Points.AddXY(16412, 1);
Chart3.Series.Add(series2);
Chart3.Width = 600;
Chart3.Height = 600;
// Series visual
series2.YValueMembers = "Frequency";
series2.XValueMember = "RoundedValue";
series2.BorderWidth = 1;
series2.ShadowOffset = 0;
series2.IsXValueIndexed = true;
// Setting the X Axis
Chart3.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;
Chart3.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Chart3.ChartAreas["ChartArea1"].AxisX.Maximum = Double.NaN;
Chart3.ChartAreas["ChartArea1"].AxisX.Title = "kbps";
// Setting the Y Axis
Chart3.ChartAreas["ChartArea1"].AxisY.Interval = 2;
Chart3.ChartAreas["ChartArea1"].AxisY.Maximum = Double.NaN;
Chart3.ChartAreas["ChartArea1"].AxisY.Title = "Frequency";
}
Now my real chart looks like this, Actual result
I would like something similar to this website :
Desired layout chart
You see, the x label is on the left, which makes way more sense considering that each column of an histogram represents the frequency of a range of values.....
Any help would be appreciated...
Did you try to add CustomLabels to replace the default ones? For example:
for (int i = 0; i <= 10; i++) {
area.AxisX.CustomLabels.Add(i + 0.5, i + 1.5, i, 0, LabelMarkStyle.None);
}
The first two are for positioning and the third would be the text value of the label.

Change Oxyplot Axisline color

I create a new Plot and its PlotModel with a black BackgroundColor
Then I create a new Axes. The X-Axis doesn't matter, it's invisible.
The Y-Axis is:
var valueAxisY = new OxyPlot.Axes.LinearAxis(AxisPosition.Left, minValue, maxValue)
{
AxislineThickness = 2,
AxislineColor = OxyColors.White,
MinorGridLinethickness = 2,
MajorGridLineThickness = 2,
MinorTickSize = 4,
MajorTickSize = 7,
TicklineColor = OxyColors.White,
FontSize = 40,
TextColor = OxyColors.White
}
Everything works BUT the Y-Axisline. It seems to stay black no matter what. The ticks starting 1-2 pixels on the left of where the line should be are white and have the correct length.
Is this the wrong parameter?
The trick was Undefined.
var valueAxisY = new OxyPlot.Axes.LinearAxis(AxisPosition.Left, minValue, maxValue)
{
LineStyle = LineStyle.Undefined
};
Somehow this is a visible, editable, continuous line.
If you define the axis line you should also hide the border arround plot area, because it is drawn over the axis line.
using model.PlotAreaBorderThickness = 0

Render OxyPlot graph in Windows Form

I would like to dock an OxyPlot graph in my windows form and graph the function y = 2x - 7. I have downloaded OxyPlot and added the references to my project. I use the following code to add the plot to my form:
public partial class GraphForm : Form
{
public OxyPlot.WindowsForms.Plot Plot;
public Graph()
{
InitializeComponent();
Plot = new OxyPlot.WindowsForms.Plot();
Plot.Model = new PlotModel();
Plot.Dock = DockStyle.Fill;
this.Controls.Add(Plot);
Plot.Model.PlotType = PlotType.XY;
Plot.Model.Background = OxyColor.FromRgb(255, 255, 255);
Plot.Model.TextColor = OxyColor.FromRgb(0, 0, 0);
}
}
With this code I see the white background, the control has been created, but it's only a white background. I've looked around the members of the OxyPlot.Plot class but I couldn't find a way to plat my equation. How can I plot my equation in the graph?
You need to add some data to display, you add this to the Models Series property.
Line (X,Y) graph example.
public Graph()
{
InitializeComponent();
Plot = new OxyPlot.WindowsForms.Plot();
Plot.Model = new PlotModel();
Plot.Dock = DockStyle.Fill;
this.Controls.Add(Plot);
Plot.Model.PlotType = PlotType.XY;
Plot.Model.Background = OxyColor.FromRGB(255, 255, 255);
Plot.Model.TextColor = OxyColor.FromRGB(0, 0, 0);
// Create Line series
var s1 = new LineSeries { Title = "LineSeries", StrokeThickness = 1 };
s1.Points.Add(new DataPoint(2,7));
s1.Points.Add(new DataPoint(7, 9));
s1.Points.Add(new DataPoint(9, 4));
// add Series and Axis to plot model
Plot.Model.Series.Add(s1);
Plot.Model.Axes.Add(new LinearAxis(AxisPosition.Bottom, 0.0, 10.0));
Plot.Model.Axes.Add(new LinearAxis(AxisPosition.Left, 0.0, 10.0));
}
This Example:

Categories