I have a simple chart, and I'd like the labels on the x-axis to be rotated 45 degrees. What am I doing wrong?
Chart c = new Chart();
c.ChartAreas.Add(new ChartArea());
c.Width = 200;
c.Height = 200;
Series mySeries = new Series();
mySeries.Points.DataBindXY(new string[] { "one", "two", "three" }, new int[] { 1, 2, 3 });
mySeries.LabelAngle = 45; // why doesn't this work?
c.Series.Add(mySeries);
The output is:
I'm using the charting stuff from System.Web.UI.DataVisualization.Charting.
The documentation says that Series.LabelAngle sets data point label angle, which (I think) is a label above the chart's column.
To set an angle of axis labels try this one:
var c = Chart1;
c.ChartAreas.Add(new ChartArea());
c.Width = 200;
c.Height = 200;
Series mySeries = new Series();
mySeries.Points.DataBindXY(new string[] { "one", "two", "three" }, new int[] { 1, 2, 3 });
//mySeries.LabelAngle = -45; // why doesn't this work?
c.Series.Add(mySeries);
c.ChartAreas[0].AxisX.LabelStyle.Angle = 45; // this works
Here is how I usually rotate my X Axis labels.
ChartArea area = new ChartArea();
area.AxisX.IsLabelAutoFit = true;
area.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30;
area.AxisX.LabelStyle.Enabled = true;
Results
The key property/line to look at above is the "LabelAutoFitStyle".
I needed these lines to get it to work:
chartarea.AxisX.LabelStyle.Angle = -90;
chartarea.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chartarea.AxisX.IsLabelAutoFit = false;
I know this question is old and answered. I just want to say that Series.LabelAngle controls series' label, not Axis'. If you add these two lines, the label would be shown above the column and is rotated 45 degrees:
mySeries.IsValueShownAsLabel = true;
mySeries.SmartLabelStyle.Enabled = false;
So you have to set AxisX's LabelAngle as Maciej Rogoziński said.
Axis.IsLabelAutoFit default value is true, so it must be set to false in order for LabelStyle.Angle to be applied.
Related
I have a couple of problems with a MSChart plotted in a Winform.
I can't get axis Y2 values displayed on the right hand side of the chart, which should be the default - similar to this question
the stripline won't plot, and
is it possible to line up the zero values of axes Y1 and Y2?
Thanks for any help.
ChartArea TestChartArea = new ChartArea();
public void CreateChartArea()
{
TestChartArea.Name = "TestChartArea";
TestChartArea.BackColor = Color.LightGreen;
TestChartArea.Position = new ElementPosition { Height = 100, Width = 80, X = 2, Y = 5 };
//TestChartArea.Position = new ElementPosition { Auto = true };
TestChartArea.AxisY = new Axis
{
Enabled = AxisEnabled.True,
IsLabelAutoFit = true,
IsMarginVisible = true,
LabelStyle = new LabelStyle { Format = "P2", ForeColor = Color.DarkBlue, Font = new Font("Arial", 10, FontStyle.Regular) },
LineColor = Color.Black,
MajorGrid = new Grid { LineColor = Color.White, LineDashStyle = ChartDashStyle.Solid },
MajorTickMark = new TickMark { LineColor = Color.Black }
};
TestChartArea.AxisY2 = new Axis
{
Enabled = AxisEnabled.True,
IsLabelAutoFit = true,
IsMarginVisible = true,
LabelStyle = new LabelStyle { Format = "P2", ForeColor = Color.DarkBlue, Font = new Font("Arial", 10, FontStyle.Regular) },
LineColor = Color.Transparent,
MajorGrid = new Grid { LineColor = Color.Yellow, LineDashStyle = ChartDashStyle.Solid },
MajorTickMark = new TickMark { LineColor = Color.Blue }
};
TestChartArea.AxisX = new Axis
{
Enabled = AxisEnabled.True,
Crossing = 0,
LineWidth = 1,
IsLabelAutoFit = true,
IsMarginVisible = false,
LabelStyle = new LabelStyle { Angle=-45,Format = "N0", ForeColor = Color.Black, Font = new Font("Arial", 8, FontStyle.Regular) },
LineColor = Color.Black,
MajorGrid = new Grid { LineColor = Color.White, LineDashStyle = ChartDashStyle.Solid },
MajorTickMark = new TickMark { LineColor = Color.LightGray, Size = 4.0f },
Name="Spot"
};
}
public void PlotChart()
{
int[] Xseries = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double[] AXJO = { 0.0025, 0.0015, -0.001, 0.002, 0.0045, -0.002, -0.003, 0.0001, -0.004, -0.0075 };
double[] ES = { 0.0020, 0.0010, -0.0005, 0.003, 0.0025, -0.001, -0.0015, 0.0005, -0.0032, -0.006 };
double[] Diff = new double[10];
// pair return
for (int i = 0; i < 10; i++)
{
Diff[i] = AXJO[i] - ES[i];
}
TestChart.BackColor = Color.LightGoldenrodYellow;
TestChart.BackSecondaryColor = Color.LightBlue;
if (TestChart.Series.IsUniqueName("AXJO"))
{
TestChart.Series.Add("AXJO");
TestChart.Series["AXJO"].YAxisType = AxisType.Primary;
TestChart.Series["AXJO"].Color = Color.Green;
TestChart.Series["AXJO"].ChartType = SeriesChartType.Line;
TestChart.Series["AXJO"].Points.DataBindXY(Xseries, AXJO);
TestChart.Series["AXJO"].ChartArea = "TestChartArea";
}
if (TestChart.Series.IsUniqueName("ES"))
{
TestChart.Series.Add("ES");
TestChart.Series["ES"].YAxisType = AxisType.Primary;
TestChart.Series["ES"].Color = Color.Red;
TestChart.Series["ES"].ChartType = SeriesChartType.Line;
TestChart.Series["ES"].Points.DataBindXY(Xseries, ES);
TestChart.Series["ES"].ChartArea = "TestChartArea";
}
if (TestChart.Series.IsUniqueName("Diff"))
{
TestChart.Series.Add("Diff");
TestChart.Series["Diff"].YAxisType = AxisType.Secondary;
TestChart.Series["Diff"].Color = Color.Blue;
TestChart.Series["Diff"].ChartType = SeriesChartType.Line;
TestChart.Series["Diff"].Points.DataBindXY(Xseries, Diff);
TestChart.Series["Diff"].ChartArea = "TestChartArea";
}
}
public void AddStripLine()
{
// add stripline at Diff=zero
StripLine ZeroDiff = new StripLine();
ZeroDiff.ForeColor = Color.Black;
ZeroDiff.BackColor = Color.Black;
ZeroDiff.StripWidth = 1;
ZeroDiff.BorderWidth = 2;
ZeroDiff.Interval = 0;
ZeroDiff.IntervalOffset = 10;
TestChart.ChartAreas["TestChartArea"].AxisY2.StripLines.Add(ZeroDiff);
}
private void button1_Click(object sender, EventArgs e)
{
PlotChart();
AddStripLine();
}
}
You asked about the unwanted placement of the AxisY2 by setting its Crossing property to 0
Simply not setting it, thus leaving it at the default double.NaN for automatically placing it at the right will resolve the issue..:
Crossing = double.NaN
To make a stripline show, it needs to start within the visible range of its axis. Offsetting it by 10 is way overboard for your data.. Also making it black is probably not what you want, unless you only want a thin line, not a colored area..
The basic rules for StripLines are:
When Interval = 0 only one stripline is shown at IntervalOffset with a width/height of StripWidth
When Interval > 0 many striplines are shown, going over the whole axis; unless you have a semi-tranparent color you need to make sure that StripWidth < Interval or else there is no space between them!
All measures are in axis-values; the types can be set with StripWidthType and IntervalType; useful especially when using one of the DateTime units.
To make the 0 values of both y-axes align, you need to tweak the Minimum and/or Maximum values of one or both axes. This may be a bit tricky and you will probably need to look into your data and take full control of spacing and placing the axis labels..
In my project im having a point chart .For each and every 1 second time interval im updating the values in the points chart that's works fine.But while updating the values it is automatically splitting 4 values on x axis like 1,2,3,4 (1,2,3,4 are x axis values).But i want 10 fixed values to be plot on x axis.How to do it?
Please refer my code below ,
series.Name = "Series";
series.Color = System.Drawing.Color.Green;
series.IsVisibleInLegend = false;
series.IsXValueIndexed = true;
series.YAxisType=AxisType.Primary;
series.ChartType = SeriesChartType.Point;
this.chart.Series.Add(series);
chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisY2.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisX.Title = "X Axis value";
chart.ChartAreas[0].AxisY.Title = "Y Axis value1";
chart.ChartAreas[0].AxisY2.Title = "Y Axis value2";
chart.ChartAreas[0].AxisX.ScrollBar.Size = 15;
chart.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
chart.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
chart.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
chart.ChartAreas[0].AxisX.ScaleView.Zoom(2,3);
chart.ChartAreas[0].CursorX.IsUserEnabled = true;
chart.ChartAreas[0].CursorY.IsUserEnabled = true;
chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart.ChartAreas[0].AxisY2.ScaleView.Zoomable = true;
chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
I am assuming you want your x axis shows 0-10 range:
Try add follow code:
chart.ChartAreas[0].AxisX.Maximum = 10;
chart.ChartAreas[0].AxisX.Minimum = 0;
If you use horizontal scroll bar for your chart,use the following code to display 10 intervals on x-axis.
chart.ChartAreas[0].AxisX.ScaleView.Zoom(2, 9);
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.
I'm trying to draw a threshold (1 line from left to right). To do so I add the threshold value twice and the value 0 and 1 for xValues then set the scale of X to be from 0 to 1.
public static void AddThreshold(Chart xlChart, double value, string name, int color, bool secondary)
{
Series series = xlChart.SeriesCollection().NewSeries();
series.Name = name;
series.XValues = new List<int>() { 0, 1 }.ToArray();
series.Values = new List<double>() { value, value }.ToArray();
Axis xAxis, yAxis;
if (secondary)
{
series.AxisGroup = XlAxisGroup.xlSecondary;
xAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlSecondary);
yAxis = (Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlSecondary);
}
else
{
xAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
yAxis = (Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
}
xAxis.MinimumScale = 0;//getting COM error here
xAxis.MaximumScale = 1;
yAxis.Delete();
series.ChartType = XlChartType.xlXYScatterLinesNoMarkers;
series.MarkerSize = 3;
series.Border.LineStyle = XlMarkerStyle.xlMarkerStyleDash;
series.Border.Color = color;
}
But I'm always getting a COM error and can't figure out the problem. To make things more annoying the exact method was working in a different project (don't ask about it because the code there was partially deleted by mistake and I am not rewriting it).
You can't set min and max of an axis if it's not a value type of axis, and only XY Scatter charts have an X axis that is a value axis. You need to specify the chart type of the added series as XY Scatter before setting the axis scale.
So move this:
series.ChartType = XlChartType.xlXYScatterLinesNoMarkers;
above this:
xAxis.MinimumScale = 0;
I don't understand something. If i don't use the customlabels, the chart will use the default label. And then if I move the scrollbar , the chart size won't adjust. The Chart view maintain the original size.
But if I use this code to change the label at row 0. (other rows don't have this problem)
chart1.ChartAreas[0].AxisY2.CustomLabels.Add((i) ,
(i+1), (ntemp * 10).ToString(), 0, LabelMarkStyle.SideMark);
And Move the scrollbar, the chart View will be a little different for size. The chart will flicker, and I don't want it.
Thanks in advance.
Here is example
Random rand = new Random();
chart1.Series.Clear();
var series = chart1.Series.Add("My Series");
series.ChartType = SeriesChartType.RangeBar;
series.Color = Color.Black;
series.YAxisType = AxisType.Secondary;
for (int i = 10; i > 2; i--)
series.Points.AddXY(i, (rand.Next(3600, 7200)), (rand.Next(30000, 80000)));
var chartArea = chart1.ChartAreas[series.ChartArea];
chartArea.BorderDashStyle = ChartDashStyle.Solid; //最外圍的框框
chartArea.BorderWidth = 10;
chartArea.AxisY.Enabled = AxisEnabled.False;
chartArea.AxisY2.Enabled = AxisEnabled.True;
chartArea.AxisY2.LabelStyle.IntervalType = DateTimeIntervalType.Number;
chartArea.AxisY2.Interval = 3600;
chartArea.AxisY2.Minimum = 0;
chartArea.AxisY2.Maximum = 86400;
chartArea.AxisY2.ScaleView.Zoom(0, 3600 * 4);
for (int i = 0; i <= 24 * 6; i++)
{
int ntemp = i % 6;
if (ntemp != 0)
{
/*Problem Here !!*/
//chart1.ChartAreas[0].AxisY2.CustomLabels.Add((i) * 600, (i + 1) * 600, (ntemp * 10).ToString(), 0, LabelMarkStyle.Box);
}
}
chartArea.CursorY.AutoScroll = true;
chartArea.AxisY2.ScaleView.Zoomable = true;
chartArea.AxisY2.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chartArea.AxisY2.ScrollBar.IsPositionedInside = false;
}
Well, I was intrigued about how and if this can be achieved with OxyPlot, and I think it can ...
Here's the code I've used, and here's a screenshot:
var model = new PlotModel("IntervalBarSeries") { LegendPlacement = LegendPlacement.Outside };
var temp_serie = new IntervalBarSeries
{
Title = "IntervalBarSeries 1",
FillColor = OxyColors.Black
};
var categoryAxis = new CategoryAxis
{
Position = AxisPosition.Left,
IsZoomEnabled = false, // No zoom on this axis
IsPanEnabled = false, // Right mouse move won't affect this axis
MajorGridlineStyle = LineStyle.Solid
,StartPosition = 1, EndPosition = 0 // This will reverse the order
};
var valueAxis = new LinearAxis(AxisPosition.Top)
{
MinimumPadding = 0.1, MaximumPadding = 0.1,
IsZoomEnabled = true,
MajorGridlineStyle = LineStyle.Solid,
MajorStep = 3600,
AbsoluteMinimum = 0
};
for (int i = 10; i > 2; i--)
{
temp_serie.Items.Add(new IntervalBarItem {
Start = rand.Next(3600, 7200),
End = rand.Next(30000, 80000)
});
categoryAxis.Labels.Add("Activity "+i);
}
model.Series.Add(temp_serie);
model.Axes.Add(categoryAxis);
model.Axes.Add(valueAxis);
MyPlotModel = model;
Now, I'm using MVVM and just binding to the plot model from my View with:
<oxy:Plot Model="{Binding MyPlotModel}"/>
But you can figure out how to do the same with WinForms once (if?) you decide to use OxyPlot and import it.
I'm assuming you're doing some work that is related to times, but your code obviously doesn't say so ... you could play around with the top header, and maybe set how to show the numbers (ATM, with no zoom, they overlap each other a bit. zooming with scroller solves that, but that's just because i've set the tick size to 3600 ... )