Share

ZedGraph

Tracker: Bugs

5 font size not displayed correctly - ID: 1870484
Last Update: Comment added ( jchampion )

In the WinForms control I am attempting to display a particular font and
font size, and yet they render smaller than other controls on the form
using the same font and font size. Below is the rendering code. Notice that
I set "pane.IsFontsScaled = false", which does seem to work - the font size
remains fixed regarless of the size of the graph control. But the lines
like "pane.XAxis.Scale.FontSpec.Size = Font.SizeInPoints", in which I am
attempting to use exactly the same font size in the graph as is used on the
form, do not seem to work properly. I have debugged it and verified that
these lines are passing the correct font name and size to the FontSpec
object, but they are rendered too small. To reproduce this bug, create a
Windows Form control, put a ZedGraph control on it, and hook up the code
below to render the graph. Also, put a Button on the form and notice how
the font size of the button label does not match the font size in the
chart.


private void CreateGraph(ZedGraphControl zed) {

GraphPane pane = zed.GraphPane;

// Set fonts
pane.IsFontsScaled = false;
pane.XAxis.Scale.FontSpec.Family = Font.FontFamily.Name;
pane.XAxis.Scale.FontSpec.Size = Font.SizeInPoints;
pane.YAxis.Scale.FontSpec.Family = Font.FontFamily.Name;
pane.YAxis.Scale.FontSpec.Size = Font.SizeInPoints;
pane.Title.FontSpec.Family = Font.FontFamily.Name;
pane.Title.FontSpec.Size = Font.SizeInPoints;

pane.YAxis.Type = AxisType.Date;

string[] labels = { "Panther", "Lion", "Cheetah", "Cougar", "Tiger",
"Leopard" };
double[] y = { 100, 115, 75, 22, 98, 40 };
double[] y2 = { 90, 100, 95, 35, 80, 35 };
double[] y3 = { 80, 110, 65, 15, 54, 67 };
double[] y4 = { 120, 125, 100, 40, 105, 75 };

BarItem myBar = pane.AddBar("Curve 1", null, y, Color.Red);
myBar.Bar.Fill = new Fill(Color.Red, Color.Red, Color.Red);
myBar.Bar.Border = new Border(Color.Red, 1);

myBar = pane.AddBar("Curve 2", null, y2, Color.Blue);
myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);

pane.XAxis.Scale.TextLabels = labels;
pane.XAxis.Type = AxisType.Text;

pane.Fill.Color = SystemColors.Control;
pane.Fill.Type = FillType.Solid;
pane.Chart.Fill.Color = SystemColors.Control;
pane.Chart.Fill.Type = FillType.Solid;

pane.Chart.Border.IsVisible = false;

pane.XAxis.MajorTic.Size = 0;
pane.YAxis.IsAxisSegmentVisible = false;

zed.AxisChange();
}


jmag ( jmag ) - 2008-01-13 19:40

5

Closed

None

Nobody/Anonymous

WinForms Control

None

Public


Comments ( 6 )

Date: 2008-02-16 23:10
Sender: jchampionProject Admin


The font scaling is always relative to the available space in ZedGraph.
Therefore, it is difficult to specify a fixed font size. The idea is that,
as the graph resizes, it always scales to the available size. When you
specify 18 points in ZedGraph, it will be 18 points relative to a base
size. These links might help.

http://zedgraph.org/wiki/index.php?title=How_does_the_font_and_chart_element_scaling_logic_work%3F
http://zedgraph.sourceforge.net/documentation/html/M_ZedGraph_PaneBase_CalcScaleFactor.htm

This is the intended behavior (not a bug).
John




Date: 2008-01-14 19:00
Sender: jmag


i was able to set the size of the fonts to the same size as the font on
the form by doing the following:

pane.IsFontsScaled = false;
pane.XAxis.Scale.FontSpec.Family = Font.FontFamily.Name;
Font displayFont =
pane.XAxis.Scale.FontSpec.GetFont(pane.CalcScaleFactor());
float fontScaleFactor = displayFont.Size / displayFont.SizeInPoints;
pane.XAxis.Scale.FontSpec.Size = Font.SizeInPoints * fontScaleFactor;

on inspection, the Form Graphics object uses PageUnit = Pixel and
PageScale = 1. The ZedGraph control uses PageUnit = Display and PageScale =
1. In both cases the DPI of the Graphics object is 96. If you divide 96
(DPI) / 72 (points) you get the scale factor I had to use to set the font
size correctly.

I don't understand this PageUnit stuff very well. But it seems like a bug
that you can't easily set the fonts used in the ZedGraph control to a
specific size just like you can do it on the Form. It is weird that on the
Form I can set objects to have a font size = 18 and yet when I want to set
the font in the ZedGraph control to the same thing I have to set the
FontSpec.Size equal to 24 (or different depending on the screen DPI).


Date: 2008-01-13 21:39
Sender: jmag


The XML documentation for FontSpec.Size says "The size of the font,
measured in points (1/72 inch)" but the font is created using
GraphicsUnit.World rather than GraphicsUnit.Point. I think this might be an
error or inconsistency, but I am not sure.


Date: 2008-01-13 21:25
Sender: jmag


the font is created using the line "font = new Font( _family, scaledSize,
style, GraphicsUnit.World );" i suspect that controls on a form are created
using a different GraphicsUnit, which is causing them to be sized
differently.


Date: 2008-01-13 21:13
Sender: jmag


If I set the FontSpec size to 4/3 of what I actually want, the result is
rendered OK because it gets scaled down. I must not be understanding
something about font sizing.


Date: 2008-01-13 20:57
Sender: jmag


I'm trying to debug this. I first set the font to size 18
(pane.XAxis.Scale.FontSpec.Size = Font.SizeInPoints;), and this line
completes. But when the label gets rendered, the font's Size = 18 but the
SizeInPoints = 13.5. And it is the SizeInPoints that seems to matter
because it gets rendered at 13.5. I'm not sure what is going on here.


Attached File

No Files Currently Attached

Changes ( 2 )

Field Old Value Date By
status_id Open 2008-02-16 23:10 jchampion
close_date - 2008-02-16 23:10 jchampion