Control Whitespace Around Chart Titles - charts

I'm using MS Chart to render charts to a PNG file.
There are two Chart Titles that collectively are taking an inordinate amount of space on the page because
There is significant whitespace ("padding") around each title
There is significant whitespace between the two titles
Reviewing the WinForms sample project at
http://archive.msdn.microsoft.com/mschart
I see that many aspects of the title (font, color, border, ...) can be changed but I do not see an option to control padding around an individual title, or extra whitespace between two titles.
Am I missing something?

Are you sure it is the title eating all the space? You could check that by setting a title border to see exactly where its bounds are. Regardless, try setting the InnerPlotPosition for the corresponding ChartArea manually and see if you can solve the problem that way.
// Set the plotting area position. Coordinates of a plotting
// area are relative to a chart area position.
chart1.ChartAreas["Default"].InnerPlotPosition.Auto = false;
chart1.ChartAreas["Default"].InnerPlotPosition.X = 10;
chart1.ChartAreas["Default"].InnerPlotPosition.Y = 10;
chart1.ChartAreas["Default"].InnerPlotPosition.Width = 80;
chart1.ChartAreas["Default"].InnerPlotPosition.Height= 80;

The only way I could get adequate control over title rendering was to render it myself using a PostPaintHandler.
Chart c = new Chart();
c.PostPaint += (sender, e) => { /* Use e.ChartGraphics.Graphics for title */ };

Related

Turn Off Point Labels in MS Chart for .NET

VB2010 using MS Chart: I've added one series as a point chart. On occasion there are over 1000 points and the point labels become way too jumbled. I have smart labeling ON:
cht.Series("srs").SmartLabelStyle.Enabled = True
But nonetheless it still looks bad. So I added a context menu to turn off the labels. The user can then zoom in to a spot and if they wish turn labels back on. I cant seem to figure out a way to do this without looping through ALL data points.
I can totally hide the points and labels by
cht.Series("srs").Enabled = False
But I want just the labels to be hidden and then redisplayed when the user has opted for it.
Any help appreciated.
Edit:
Since I haven't found a method to turn labels off and on with one command I am currently cycling through each point in the series.
Me.Cursor = Cursors.WaitCursor
Application.DoEvents()
'suspend updating UI
cht.Series.SuspendUpdates()
'cycle through all points in the series and set the label either to an empty string or whatever is cached in the Tag property.
'todo: this is not efficient for large datasets but its the only thing we have.
For Each pt As DataPoint In cht.Series("srs").Points
If mnuDisplayLabels.Checked Then
pt.Label = pt.Tag.ToString
Else
pt.Label = ""
End If
Next pt
'resume updating UI
cht.Series.ResumeUpdates()
'force redraw of chart
cht.Update()
I think you have to loop, but you want to suspend updating the UI until you've done it for all points. Try something like:
chart1.Series.SuspendUpdates();
foreach (Series s in chart1.Series)
{
s.IsValueShownAsLabel = false;
}
chart1.Series.ResumeUpdates();

Can I tell iText how to clip text to fit in a cell

When I call setFixedHeight() on a PdfPCell, and add more text than fits in the given height, iText seems to print the prefix of the string which fits.
Can I control this clipping algorithm? For example:
Print a suffix of the string rather than a prefix.
Mark a substring of the string as not to be removed. This is with footnote references. If I add text saying "Hello World [1]", the [1] is a reference to a footnote and should not be removed. It's okay to remove the other characters of the string, like "World".
When there are multiple words in the string, iText seems to eliminate a word that doesn't fit, while I would like it partially printed. That is, if the string is "Hello World", and the cell has room only for "Hello Wo...", I would like that to be printed, rather than just "Hello", as iText prints.
Rather than printing characters in their entirety, print only part of them. Imagine printing the text to a PNG and chopping off the top and/or bottom part of the PNG to fit it in the space available. For example, notice that the top line and the bottom line are partially clipped here:
Are any of these possible? Does iText give me any control over how text is clipped? Thanks.
This is with reference to iText 2.1.6.
I have written a proof of concept, ClipCenterCellContent, where we try to fit the text "D2 is a cell with more content than we can fit into the cell." in a cell that is too small.
Just like in your other question ( iText -- How do I get the rendered dimensions of text? ), we add the content using a cell event, but we now add it twice: once in simulation mode (to find out how much space is needed vertically) and once for real (using an offset).
This adds the content in simulation mode (we use the width of the cell and an arbitrary height):
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(new Rectangle(0, 0, position.getWidth(), -1000));
ct.addElement(content);
ct.go(true);
float spaceneeded = 0 - ct.getYLine();
System.out.println(String.format("The content requires %s pt whereas the height is %s pt.", spaceneeded, position.getHeight()));
We now know the needed height and we can add the content for real using an offset:
float offset = (position.getHeight() - spaceneeded) / 2;
System.out.println(String.format("The difference is %s pt; we'll need an offset of %s pt.", -2f * offset, offset));
PdfTemplate tmp = canvas.createTemplate(position.getWidth(), position.getHeight());
ct = new ColumnText(tmp);
ct.setSimpleColumn(0, offset, position.getWidth(), offset + spaceneeded);
ct.addElement(content);
ct.go();
canvas.addTemplate(tmp, position.getLeft(), position.getBottom());
In this case, I used a PdfTemplate to clip the content.
I also have answers to your other questions, but I don't have the time to answer them right now.
For straight Text box clipping, I adapted the C# code given here
http://itextsharp.10939.n7.nabble.com/Limiting-Text-Width-using-PdfContentByte-td2481.html
to the Java code below. The clipping area ends up outside this rectangle, so you can still draw a rectangle on the same exact coordinates.
cb.saveState();
cb.rectangle(left,top,width,height);
cb.clip();
cb.newPath();
// perform clipped output here
cb.restoreState();
I used a try/finally to ensure restoreState() was called.

How can I get the size of the Plot Area in a Microsoft Chart Control

I am using the MS Chart Control in a Windows application.
I am charting various series dynamically and the user can specify which charttype each series should be. This leads to situations where pie charts are combined with line/spline charts etc..
When at least one series is specified to be a pie chart, I am dynamically adding chartareas and legends to give each of these series their own chartarea, while all the "basic" charttypes (line(area)/spline(area)/etc.) are combined into a single chartarea.
The issue is that when adding 10+ series where the majority are pie charts, the charts are resized so small, they become useless. My thought was to dynamically increase the size of the chart control (thereby increasing the size of all chartareas within). My issue is, the width and height of the InnerPlotPosition are always zero unless I set them explicitly.
Is there any way to determine the size of the Plotting Area in pixels or percentage (which I could multiply by the chart controls size to get pixels), etc. even if not explicitly set? This would allow me to increase the chart controls size until some minimum ( and hopefully, readable) value was reached.
I find that InnerPlotPosition is set at least in a PostPaint event callback. You may be checking that property before the Plot Area has been calculated.
This works:
var c = new Chart();
c.PostPaint += c_PostPaint;
// Do whatever to setup the chart
// Then in my case I'm saving the image which causes rendering
c.SaveImage(myFileName);
void c_PostPaint(object sender, ChartPaintEventArgs e)
{
var ipp = e.Chart.ChartAreas[0].InnerPlotPosition; // Values populated here
}

matlab: having a good control of MyBox, box with text

my problem is I want to have a box for additional bigger comments to the graph.
How can I create this box, so that it will be always on the right bottom side outside of my graph, and it will be automatically adjusting its size to the amount of text inside? Also, if I will have longer text, I need it to be split into more lines, not just one line extremely long.
I use it like that, however I have a poor control over what is happening, this just creates a certain size in a certain place, text is cut.. please help :)
x = rand(110)*100;
y = x;
plot(x,y)
MyBox = uicontrol('style','text')
set(MyBox,'String','optional longer information to be put into diagram')
set(MyBox,'Position',[10,0,40,10])
I am afraid you can not do that automatically - Text boxes do not use callbacks. From the documentation:
Users cannot change static text interactively. Static text controls do not activate callback routines when clicked.
What you can do is call textwrap function, which will automatically wrap the text you pass to it and return new Text box position:
MyBox = uicontrol('style','text');
% set initial position - first three values are kept constant after wrap, the hight can be changed
set(MyBox,'Position',[10,10,30,10])
% adjust the height of your Text box and wrap the text
[outstring,newpos] = textwrap(MyBox,{'optional longer information to be put into diagram'});
% set new position and text
set(MyBox,'Position',newpos, 'String', outstring)
You will have to call textwrap and set(...) yourself whenever you change the string in the text box.

asp.net charts: Legend overlapping with X-axis

I am creating a Chart (DataVisualization.Charting.Chart) programmatically, which is a Stacked Bar chart.
I am also adding Legend entries programmatically to it. I want to show the Legend at the bottom of the chart.
But, while doing so, the Legend overlapps with the X-axis of the chart.
Here is the code I am using:
Private Function GetLegend(ByVal legendName As String, ByVal s As Single) As System.Windows.Forms.DataVisualization.Charting.Legend
Dim objLegend As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()
objLegend.Name = legendName
objLegend.Font = New System.Drawing.Font("Verdana", s)
objLegend.IsDockedInsideChartArea = False
objLegend.Docking = Docking.Bottom
Return objLegend
End Function
Below statement adds that Legend to the chart
_msChart.Legends.Add(GetLegend("SomeValue1", 10.0F))
Any idea, what is missing? I want to show the legend at the bottom only, but it should not overlapp with the X-axis.
I had the same problem today. Try adding:
objLegend.Position.Auto = true
objLegend.DockedToChartArea = "yourChartAreaName"
That did not help me but I found on the net that this might be helpful (and clean solution).
What actually worked for me was moving chart area to make space for legend so it no longer overlaps. My legend was on top so this code worked for me:
chart.ChartAreas[0].Position.Y = 15
You can try resizing it instead, forcing it to be for example 20 pixels shorter than chart.Size.
Hope this helps.
I had an overlapping legend/chart area problem as well but none of the other suggestions here seemed to make any difference. I think the problem stems from legend text wrapping to two lines and the sizing algorithms not taking account of this.
The ideas here got me thinking more clearly about the problem though, and I was able control the size and position of the chart area using the following.
Chart1.ChartAreas[0].InnerPlotPosition = new ElementPosition(15, 5, 90, 75);
There's not much intellisense on those parameters, but as well as I could deduce, the parameters are all percentages of the total chart area (I initially thought they might be pixel values and got some very odd results). So what I've written above would set the plot area to start at 15% in from the left edge of the chart image and 5% down from the top, and have a width of 90% and a height of 75%.