Custom label not aligning horizontally in ASP.NET charts - c#

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)

Related

Setting y-axis range using Interactive data display WPF

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.

How to change data label position in EPPlus?

So here is my successfully generated chart. However for some reason on this chart only it decides the date times should show up at the top of the chart instead of the bottom. Here is my code and what I've tried. Notice the text is neither bold nor is at showing where it should. I've also tried giving the chart a ton of area to work with and it still does this. Any help in the right direction would be greatly appreciated.
ExcelChart paintDewChart = overview.Drawings.AddChart(w.Name + "Dew", OfficeOpenXml.Drawing.Chart.eChartType.LineMarkersStacked);
paintDewChart.Title.Text = "Paint Dew";
paintDewChart.SetPosition(24, 0, 0, 0);
paintDewChart.SetSize(1550, 400);
paintDewChart.Legend.Position = eLegendPosition.Bottom;
var dser1 = (ExcelLineChartSerie)(paintDewChart.Series.Add(w2.Cells["C4:C" + water.Count.ToString()], w1.Cells["A4:A" + water.Count.ToString()]));
var dser2 = (ExcelLineChartSerie)(paintDewChart.Series.Add(w2.Cells["D4:D" + water.Count.ToString()], w1.Cells["A4:A" + water.Count.ToString()]));
dser1.Header = "PC2 Dew";
dser2.Header = "B Dew";
dser1.DataLabel.Position = eLabelPosition.Bottom;
dser2.DataLabel.Position = eLabelPosition.Bottom;
dser1.DataLabel.Font.Bold = true;
dser2.DataLabel.Font.Bold = true;
EDIT:
I figured out I can flip the orientation with this, however labels still overlay on the graph instead of below it.
paintDewChart.YAxis.Orientation = eAxisOrientation.MaxMin;
Here is the correct code I need for my situation.
paintDewChart.YAxis.Orientation = eAxisOrientation.MaxMin;
paintDewChart.XAxis.LabelPosition = eTickLabelPosition.High;
paintDewChart.XAxis.TickLabelPosition = eTickLabelPosition.High;
EDIT: Also depending on minimum and maximum value ranges going from positive to negative, you may need this.
paintDewChart.XAxis.Crosses = eCrosses.Max;

Setting color of pie chart slices and legend

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.

How to manually position ASP.Net Chart Legend?

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.

Programmatically assigning Margin and/or Padding to a Label

In trying to get some labels in a TableLayoutPanel to move from the top left of their cells to the center of the cells, I'm trying to experiment with adding padding and/or margins.
However, nothing I've tried works. Here's the code I've tried and the results:
// Setting the padding just cuts off the bottom part of the text
//lbl.Padding = new System.Windows.Forms.Padding(1);
// How to set Margin?
//lbl.Margin = new System.Windows.Forms.Margin(1); <- This mimics "Padding" but is not recognized
//lbl.Margin = new Thickness(6); <- This is the only example I could find, but it's for WPF
Try:
lbl.Margin = new Padding(1);
You might also want to do:
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.AutoSize = false;
labelName.Style.Add("Margin", "10px");

Categories

Resources