Using the Chart controls built into ASP.Net, I'm trying to manually position the Title and the Legend so that they are directly next to each other horizontally just above the ChartArea. I've been able to manually position the Title using the following code:
chart.Titles["Title1"].Position.Auto = false;
chart.Titles["Title1"].Position.X = 10;
chart.Titles["Title1"].Position.Y = 5;
There's nothing to it, really. However, I'm attempting to position the Legend to the right of it with the following code, and the Legend doesn't even appear:
chart.Legends["Legend1"].Position.Auto = false;
chart.Legends["Legend1"].Position.X = 30;
chart.Legends["Legend1"].Position.Y = 5;
Any ideas what I'm doing wrong? This seems like it should be relatively simple. I've even tried various other coordinates, and I can't get the Legend to appear anywhere. It does appear if I use the built-in positioning such as below, but this positioning does not suit my purposes:
chart.Legends["Legend1"].Docking = Docking.Top;
chart.Legends["Legend1"].DockedToChartArea = "ChartArea1";
chart.Legends["Legend1"].IsDockedInsideChartArea = false;
chart.Legends["Legend1"].Alignment = StringAlignment.Far;
Try newing up an ElementPosition object, like this:
chart.Legends["Legend1"].Position.Auto = false;
chart.Legends["Legend1"].Position = new ElementPosition(30, 5, 100, 20);
Note: The constructor for ElementPosition takes 0 or 4 parameters (x, y, width, height).
I stumbled on this question for looking how to move legend at the bottom of a chart.
Answer for that is to use Docking property
Chart1.Legends["Legend1"].Docking = Docking.Bottom;
It may be helpful for someone in future, as this is the first link in google search.
Related
I'm stuck on a problem where i need to set y-axis based on its plot. Right now program stretches the plot to fit whole chart however i need it to let some free space above the plot. I wasn't able to come up with anything so far. Code looks like this:
Chart chart = new Chart()
{
LegendVisibility = Visibility.Hidden,
LeftTitle = YaxisName[i],
BottomTitle = "Carrier i [-]",
};
chart.Content = getChart(graphIndex, GraphYvalues, GraphXvalues, GraphListOfNames[graphIndex]);
chartView.Children.Add(chart);
Could someone please help me out? Thank you
Eventually I was able to scramble something up. It's actually so damn easy. I gave up on setting the range of y-axis and instead of that I added padding to the plot itself like this:
LineGraph lineGraphFar = new LineGraph()
{
Stroke = new SolidColorBrush(Colors.Red),
Padding = new System.Windows.Thickness(0, 30, 0, 0)
};
It's surely not the greatest option but it will do the job.
I am using Chart control from .NET framework in my project. I have added chart control to the form and configured as shown below.
// Add a new series.
chart1.Series.Add("1");
var series = chart1.Series[0];
series.ChartType = SeriesChartType.Spline;
// Hide the legend.
series.IsVisibleInLegend = false;
// configure x axis.
var cArea = chart1.ChartAreas[0];
cArea.AxisX.IntervalType = DateTimeIntervalType.Number;
cArea.AxisX.LabelStyle.Format = "00";
cArea.AxisY.LabelStyle.Format = "0.000";
cArea.AxisY.LabelStyle.IsEndLabelVisible = true;
cArea.AxisX.Minimum = 0;
cArea.AxisX.Maximum = 100;
cArea.AxisX.Interval = 20;
cArea.AxisY.Minimum = 0;
cArea.AxisY.Maximum = 100;
cArea.AxisX.Interval = 20;
Data point values are as below:
chart1.Series[0].Points.AddXY(0, 5);
chart1.Series[0].Points.AddXY(5, 10);
chart1.Series[0].Points.AddXY(10, 30);
chart1.Series[0].Points.AddXY(20, 100);
chart1.Series[0].Points.AddXY(30, 100);
chart1.Series[0].Points.AddXY(40, 90);
chart1.Series[0].Points.AddXY(50, 80);
For the above data points, series is not smooth. Upper edge is getting cut. Refer attached image.
How to make it smooth so that whole line is visible ?
It's not visible because of the smoothing, adapt the scale (using cArea.AxisX.Maximum = 150; for example) or remove the smoothing to make the whole curve visible.
As with the DrawCurves GDI+ method you can control the tension of the splines, i.e. how close they are to the points and their connecting lines and how much smoothing they create. Too much 'smoothing' creates the fantasy tops you see and also crazy whirls from even small bumps in the data..
Setting the tension it is done via the LineTension Custom attribute.
Lower it from the default of 0.8 to something smaller. Test to see what you prefer.
Here is an example for a Series S :
S.SetCustomProperty("LineTension", "0.4");
Note that you still should make the y-axis Maximum a little larger or else you may need to bring the tension down to 0, which will look like a line type..
Here are a few variations:
Does anybody know how to set the color of the slice/legend of a PowerPoint 2010 Pie Chart in C#? I can't make anything I read from the MSDN site work. I can't work out the proper way to get the correct object and property.
Edit:
Well, I thought about adding code, but what I have is not working, so I wasn't sure how helpful or informative it would be. I can't figure out which object/method/property to use to get access to the pie chart slice color. I've tried Point, LegendKey, LegendEntry, Legend, and associated methods/properties with each. I've tried many things that aren't even represented in my code anymore.
But, for what it's worth, this is what my code is now:
PowerPoint.Series series = (PowerPoint.Series)xlChart.SeriesCollection(1);
PowerPoint.Point point = (PowerPoint.Point)series.Points(xlCol);
point.Interior.ColorIndex = Convert.ToInt32(dataArray[2, i]);
PowerPoint.Legend legend = (PowerPoint.Legend)xlChart.Legend;
PowerPoint.LegendEntry lentry = (PowerPoint.LegendEntry)legend.LegendEntries(1);
Interior.ColorIndex won't work, because there are only two values in the enumeration: xlColorIndexAutomatic and xlColorIndexNone.
You were pretty close, however. What you want is Interior.Color. I use hex to set the color, but I'm sure there are other ways. The example below was based on assumptions that there is an existing PowerPoint file with a pie chart on first slide and nothing else. Obviously, you'd adjust it to your conditions.
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace SampleApp
{
class Program
{
static void Main(string[] args)
{
var filePath = #"C:\users\userx\desktop\test.pptx";
var app = new PowerPoint.Application();
var presentation = app.Presentations.Open(filePath);
var slide = presentation.Slides[1];
var chart = slide.Shapes[1].Chart;
var series = chart.SeriesCollection(1) as PowerPoint.Series;
var point = series.Points(1) as PowerPoint.Point;
point.Interior.Color = 0x41BA5D;
point = series.Points(2) as PowerPoint.Point;
point.Interior.Color = 0xA841BA;
point = series.Points(3) as PowerPoint.Point;
point.Interior.Color = 0xBA4141;
point = series.Points(4) as PowerPoint.Point;
point.Interior.Color = 0x7AB4FF;
}
}
}
The original pie chart looked like so:
While the new chart had this appearance:
As I've mentioned, there are many ways to set the colors and I showed you the hex way. If you reference System.Drawing assembly, then you'll have access to Color, which simplifies things a lot:
var point = series.Points(1) as PowerPoint.Point;
point.Interior.Color = Color.Red;
point = series.Points(2) as PowerPoint.Point;
point.Interior.Color = Color.Pink;
point = series.Points(3) as PowerPoint.Point;
point.Interior.Color = Color.Black;
point = series.Points(4) as PowerPoint.Point;
point.Interior.Color = Color.Green;
The legend entries will change their color accordingly, so if you're using this approach, you don't even have to worry about setting color there.
As you can tell, Interop can be a pain. Hope this clears some things up for you.
Provided you are properly referencing Microsoft.Office.Interop.PowerPoint object library (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.aspx), changing the Pie Chart Points Color could be done as shown in the following C# code sample:
xlChart.Series[0].Points[0].Color = Color.Red;
xlChart.Series[0].Points[1].Color = Color.Blue;
Hope this will help.
I am binding datatable to ASP.NET chart for which I am using custom label for X-Axis. The label text is aligned vertically and the chart size gets reduced.
I tried the following code :
Chart2.ChartAreas[0].AxisX.IsLabelAutoFit = false;
Chart2.ChartAreas[0].AxisX.LabelStyle.Enabled = true;
Chart2.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Comic Sans", 9F);
Chart2.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
I also tried the following fix by searching the internet:
Chart2.ChartAreas[0].AxisX.LabelStyle.Angle = 90;
Chart2.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep90;
I tried changing various values for the angle but still it would align vertically.
Any links or suggestion on this would be much appreciated.
I am Ultra New, but this may help:" i have been searching for a solution for the last 4 hrs and someone suggested using annotations. Also this code is in VB, but it may help with another direction if you can't find an answer.
Dim MyTextAnnotation As TextAnnotation
MyTextAnnotation = New TextAnnotation
MyTextAnnotation.Text = "some notation"
MyTextAnnotation.X = 0
MyTextAnnotation.Y = 0
chart1.Annotations.Add(MyTextAnnotation)
using the MS Charting for .NET, I am trying to zoom into the chart which I have created.
This works fine on the Y axis (type = float) and on the X axis if type = int, but when I have DateTime values on the X axis, scrolling does not behave as it should on this axis.
Vertically, everything still behaves properly, but while I can zoom into the X axis, I cannot drag the sliding bar to move where I am zoomed into. However, I can click either side and it will jump.
Does anyone know how to fix this and make it behave like it does with float values?
Thanks!
Depending on your data, try setting the chart area's CursorX.IntervalType property to something other than Auto.
You may run into a similar issue when trying to use the small scroll arrows of the scroll bar once you are zoomed in. To fix that you can try to set the chart area's AxisX.ScaleView.SmallScrollSizeType property to the same thing as the CursorX.IntervalType.
For example, if you have a chart with data that is reported every 30 seconds you can use the following settings:
chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].CursorX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chart1.ChartAreas[0].CursorX.Interval = 0.5D;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Minutes;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 0.5D;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss";
I had the same problem and these settings solve it for me:
_chart.ChartAreas[0].CursorX.IsUserEnabled = true;
_chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
_chart.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].CursorX.Interval = 1D;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 1D;
_chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
_chart.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].AxisX.ScaleView.MinSize = 1D;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1D;
Especially the last two lines did the job.
add
chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;
My solution was:
chart1.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Milliseconds;