I need to draw a histogram, and columns with minimum and maximum values should have other colors that are different from other columns and have different widths!
Now I have done this in two sets, but it's the wrong outcome.
Below is my code:
chart1.Series[0].Color = Color.Blue;
int y;
for (y = 0; y < lbls.Length; y++)
{
chart1.Series[0].Points.AddY(costs[y]);
chart1.ChartAreas[0].AxisX.CustomLabels.Add(new CustomLabel(y, y + 2, lbls[y], 0, LabelMarkStyle.LineSideMark));
}
chart1.Series[1].Color = Color.Red;
chart1.Series[1].Points.AddY(1500);
chart1.ChartAreas[1].AxisX.CustomLabels.Add(new CustomLabel(y+4, y + 6, "lol", 0, LabelMarkStyle.LineSideMark));
its give me:
Not too sure how to change specific column width but and to change a specific i points color do the below.
'Loop through series to find min and max and then color label
Chart1.Series[0].Points[i].Color = Color.Red
P.S.
I'm not sure if you can change just one columns width. I sometimes use Chart1.Series[0]("PixelPointWidth") = some integer to change the width of column series but looking at the msdn page it seems it can only be applied to the series and not individual datapoints.
PixelPointWidth # MSDN
You could just create a new series and add it to the chartarea with just the max and min values and then do Chart1.Series[1]("PixelPointWidth") = some integer to make them wider than the first series
Related
I use the C# Chart in WinForms to plot a variety of variables in real time using the "line" chart type. That works well for analog values, but it's less than ideal for on/off flags.
I'd like to plot multiple flags as horizontal bars that are filled when the value is '1" and clear when the value is '0'.
Before I start coding a solution from scratch, do you have any suggestion on how I could take advantage of any features of the "chart" object to implement this more effectively?
EDIT: I am playing with the Area type, and it seems to be promising.
EDIT 2: That didn't work, because the area in the Area type always starts at the bottom of the chart, hiding the other rows. I am now trying the Range Column type
There are several ways to tackle this.: StackedBars, AreaChart, Annotations but I think by far the simplest is using a LineChartType.
The first issue is: How to create the gaps? The simplest way is to draw them as lines but with Color.Transparent. So instead of using the flag value as our y-value we use it to set the color..
So we could use a function like this:
void AddFlagLine(Chart chart, int series, int flag, int x)
{
Series s = chart.Series[series];
int px = s.Points.AddXY(x, series);
s.Points[px].Color = s.Color;
if (px > 0) s.Points[px - 1].Color = flag == 1 ? s.Color : Color.Transparent;
}
It takes the index of your Series and uses the flag to determine the color; note that the color of a line segment is controlled by the color of the end point.
So if you want to have the line going out from the new point to have its flag color, you need to set it when adding the next one..
This is simple enough and for lines as thick as 1-10 it works fine. But if you want larger widths things get a bit ugly..:
The rounded caps start to get bigger and bigger until they actually touch, flling the gaps more or less.
Unfortunately there seems to be no way to controls the caps-style of the lines. There are many CustomAttributes including DashStyles but not this one. So we have to resort to owner-drawing. This is rather simple for line charts. Here is an example:
The xxxPaint event looks like this:
private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
Graphics g = e.ChartGraphics.Graphics;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
for (int si = 0; si < chart.Series.Count; si++ )
{
Series s = chart.Series[si];
for (int pi = 1; pi < s.Points.Count - 1; pi++)
{
DataPoint dp = s.Points[pi];
int y = (int) ay.ValueToPixelPosition(dp.YValues[0]+1); ///*1*
int x0 = (int)ax.ValueToPixelPosition(ax.Minimum);
int x1 = (int)ax.ValueToPixelPosition(s.Points[pi-1].XValue); ///*2*
int x2 = (int)ax.ValueToPixelPosition(dp.XValue);
x1 = Math.Max(x1, x0);
x2 = Math.Max(x2, x0);
using (Pen pen = new Pen(dp.Color, 40) ///*3*
{ StartCap = System.Drawing.Drawing2D.LineCap.Flat,
EndCap = System.Drawing.Drawing2D.LineCap.Flat })
{
g.DrawLine(pen, x1, y, x2, y);
}
}
}
A few notes:
1 : I have decided to move the the series up by one; this is up to you just as using or turning off the y-axis labels or replacing them by custom labels..
2 : Here we use the previous point's x-position!
3 : Note that instead of hard coding a width of 40 pixels you really should decide on a calculated width. This is an example that almost fills up the area:
int width = (int)( ( ay.ValueToPixelPosition(ay.Minimum) -
ay.ValueToPixelPosition(ay.Maximum)) / (chart7.Series.Count + 2));
You can twist is to fill more or less by adding less or more than 2.
I have turned all BorderWidths to 0 so only the drawn lines show.
I got it:
It turned out to actually be pretty easy; I used the Range Column type.
A) Set-up (done once):
plotChart.Series[chanNo].ChartType = SeriesChartType.RangeColumn;
plotChart.Series[chanNo].CustomProperties = "PointWidth=" + noOfFlags;
PointWidth is required to set the relative width of each rectangle so that it fills the entire width of one data point (if too small, there are gaps in the horizontal bar; if too large, there is overlap). noOfFlags is the number of flags shown (in the example shown above, noOfFlags = 4). (By the way the MSDN documentation is wrong: PointWidth is not limited to 2.)
B) Plotting (done for each new data point):
baseLine--;
int barHeight = flagHigh ? 1 : 0;
plotChart.Series[chanNo].Points.AddXY(pointX, baseLine, baseLine + barHeight);
flagHigh is a bool that is equal to the flag being monitored.
baseLine is decremented for each trace. In the example above, baseLine starts at 4, and is decremented down to 0.
Note that for each data point, RangeColumn requires 2 "Y" values: one for the bottom of the rectangle, one for the top; in the code, I set the bottom Y to the bottom of the row that I use for that particular flag, and the top to 1 above the bottom, to give me a height of 1.
I'm using EPPlus and C# and trying to autosize/autofit the height of a row to accommodate the height needed to show all of the contents of a merged cell with text wrapping. However no matter what I try the text always truncates. Since I'm repeating this process with various text sizes on various worksheets, I don't want to hard code the row height (except to enforce a minimum height for the row). If possible I'd like to do this within EPPlus/C#.
With the cells A2:E2 merged and WrapText = true:
Cell with Text Truncated
Here's what it should look like with desired Cell Height
Here's my relevant and short C# code
Int32 intToCol;
intToCol = 5;
eppWorksheet.Cells[2, 1, 2, intToCol].Merge = true;
eppWorksheet.Cells[2, 1].Style.WrapText = true;
//Check if at the minimum height. If not, resize the row
if (eppWorksheet.Row(2).Height < 35.25)
{
eppWorksheet.Row(2).Height = 35.25;
}
I've looked at Autofit rows in EPPlus and it didn't seem to directly answer my question unless I'm reading it wrong.
Here is the solution in a reusable method. Pass in the text value, font used for the cell, summed width of the columns merged, and receive back the row height. Set the row height with the result.
Use of Method
eppWorksheet.Row(2).Height = MeasureTextHeight(cell.Value, cell.Style.Font, [enter the SUM of column widths A-E]);
Reuseable Method
public double MeasureTextHeight(string text, ExcelFont font, double width)
{
if (text.IsNullOrEmpty()) return 0.0;
var bitmap = _bitmap ?? (_bitmap = new Bitmap(1, 1));
var graphics = _graphics ?? (_graphics = Graphics.FromImage(bitmap));
var pixelWidth = Convert.ToInt32(width * 7); //7 pixels per excel column width
var fontSize = font.Size * 1.01f;
var drawingFont = new Font(font.Name, fontSize);
var size = graphics.MeasureString(text, drawingFont, pixelWidth, new StringFormat { FormatFlags = StringFormatFlags.MeasureTrailingSpaces });
//72 DPI and 96 points per inch. Excel height in points with max of 409 per Excel requirements.
return Math.Min(Convert.ToDouble(size.Height) * 72 / 96, 409);
}
I have used a workaround for this and I a had print area A:Q.
I copied merged cells value to column z.
set width of column z to merge cells width.
Then set auto row height true in format.
Hide the z column.
Set print area A:Q
Cons:
There are duplicate data. But we are okay since report is printing and not print z column.
Pros:
Row height works correctly not like calculation method.
Had to tweak the code a little bit by removing the multiplication factor at the return line. May be because i am using this code to get the width of the column.
ws1.Column(colIndx).Width * 7
The multiplication factor is the number of columns been merged.
I have a control square with user defined height, top range and bottom range values.
There is a line in between the control which divides the top range and bottom range.
User will provide another value "fillValue" too.
Problem:
For instance:
height = 100;
topRange = 10;
bottom range= 10;
fillValueTop= 5;
Now here I have to decide what percentage 5 will be for the toprange. Basically the height of fill for upper side of the square.
with same height, toprange and bottomrange, the user can change fillValueTop = 40. In this case
what percentage should get filled for the upper side of the square?
Similarly, it could be,
height = 200;
topRange = 15;
bottomrange= 15;
fillValueBottom= 20;
Basically I need to calculate the percentage of fill depending on the above parameters.
Can someone help me with some formular or so.
I thought something like this.
var unit = height/(topRange + bottomrange);
var actualFillValue = unit*fillValueTop;
But I think it does not work.
Thanks & Regards,
I'm trying to use the chart control on a windows form and have it working, plotting some real time data, however before the data arrives nothing is displayed. I would like to show an empty graph with an X Y of 10 30 but still have the graph auto range if values go above this.
I cannot find a property to show the "blank" graph it this possible and if so how?
thanks
You can hide all data of a Series by making its line color Transparent. If you also set its LegendText to be " " all you can see are the Axis ticks. you can control them by adding a few Points and by setting the Minimum and Maximum values:
// short reference for our dummy:
Series S0 = chart1.Series[0];
// a simple type
S0.ChartType = SeriesChartType.Line;
// set 10 point with x-values going from 0-100 and y-values going from 1-10:
for (int i = 0; i < 100; i +=10) S0.Points.AddXY(i , i / 10);
// or add only a few, e.g. the first and last points:
//S0.Points.AddXY(100, 10);
//S0.Points.AddXY(0, 10);
// hide the line:
S0.Color = Color.Transparent;
// hide the legend text (it will still take up a little space, though)
S0.LegendText = " ";
// limit the axis to the target values
chart1.ChartAreas[0].AxisX.Maximum = 100;
chart1.ChartAreas[0].AxisX.Minimum = 0;
The result looks like an empty chart:
I am new to iTextSharp, and want to convert my DataGridView to a PdfPtable.
When i write the table to PDF, it shows up nicely, except when the amount of columns is relatively large. Then it accomodates by making the column width smaller.
Instead, I would like to start on a new page, with more of the table shown, with headers.
Below is my code for the table:
PdfPTable table = new PdfPTable( view.Columns.Count );
float[] widths = new float[view.Columns.Count];
for ( int i = 0; i < view.Columns.Count; i++ ) {
widths[i] = view.Columns[i].Width;
}
table.SetWidths( widths );
table.HorizontalAlignment = 1; //Center
PdfPCell cell = null;
//Headers
foreach ( DataGridViewColumn c in view.Columns ) {
cell = new PdfPCell( new Phrase( new Chunk( c.HeaderText, _standardFont ) ) );
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell( cell );
}
//Rest of table
for ( int i = 1; i < view.Rows.Count; i++ ) {
for ( int j = 0; j < view.Columns.Count; j++ ) {
bool hasValue = view.Rows[i].Cells[j].Value == null ? false : true;
if ( hasValue ) {
cell = new PdfPCell( new Phrase( view.Rows[i].Cells[j].Value.ToString(), _standardFont ) );
} else {
cell = new PdfPCell( new Phrase( "", _standardFont ) );
}
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell( cell );
}
}
One solution would be to somehow know how many columns could be printed fullsize on a page, and then add a new page, with more of the grid displayed there, but i am unsure how to detect a new page. The problem here is that there seems to be no way of changing the number of columns of a PdfPtable, so I have to be able to calculate the effect in width when adding columns beforehand.
EDIT: I want to split the pages vertically
The problem you're facing, is the fact that PDF isn't HTML. A page in a PDF has a fixed size. You don't have a scrollbar, and the content doesn't change when you resize the viewer window.
I'm going to answer using JAVA examples from Chapter 4 of my book. If you need the C# version of these examples, you'll need this URL: http://tinyurl.com/itextsharpIIA2C04
First this: whether or not a table looks nice is something a machine can't judge. It's something only a human can do.
Seems like you want to control the width of the columns. The example ColumnWidths gives you some examples on how to do that. In your case, you should use a solution that involves using absolute values for the width of the table. You'll recognize these solutions because they all have this line in common:
table.setLockedWidth(true);
Using an absolute width is mandatory if you want to calculate the height of each row or of the complete table. A Frequently Asked Question is "I've created a table and when I ask for its total width, it returns zero." It can be answered by a counter-question: "How can iText calculate the height of a table, if you didn't define the width first?" This is a rhetorical question: it's impossible to calculate the height before the width has been defined. That's shown in the TableHeight example.
Once you've defined the widths of the table (and its columns), you can calculate the height, and you're ready to position the table at absolute positions. The Zhang example shows how it's done. In this example, a table with 4 columns is created. The first two colums are drawn using this commando:
table.writeSelectedRows(0, 2, 0, -1, 236, 806, canvas);
Where the first 0 defines the first column that needs to be drawn, and the 2 defines the first column that isn't drawn. The remaining columns are drawn on the next page, using this commando:
table.writeSelectedRows(2, -1, 0, -1, 36, 806, canvas);
Where 2 defines the first column that needs to be drawn, and -1 indicates that all remaining columns should be drawn.
In this case, 0 and -1 are used for the rows. If your table contains more rows than fit on one page, you'll have to do the math to make sure you don't have any rows that "fall off the page."
Note that the 36 and 806 represent an X and a Y coordinate. As your taking control over the layout, you're responsible to calculate those coordinates.