can you tell me what is wrong with my code? I want to generate chart image in console application over System.Windows.Forms.DataVisualization.Charting library... Following code generate me chart only with columns, but I need chart with axis. Any ideas?
Chart chart = new Chart();
chart.Size = new System.Drawing.Size(2000, 500);
ChartArea area = new ChartArea();
chart.ChartAreas.Add(area);
chart.BackColor = System.Drawing.Color.Transparent;
chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chart.ChartAreas[0].AxisX.Title = "sasdasdasd";
Series series = new Series()
{
Name = "series2",
IsVisibleInLegend = false,
ChartType = SeriesChartType.Column
};
chart.Series.Add(series);
foreach (CnbItem item in items)
{
DataPoint p1 = new DataPoint(0, Double.Parse(item.Kurz));
p1.Color = System.Drawing.Color.LightBlue;
p1.AxisLabel = item.Kod;
p1.LegendText = item.Kod;
p1.Label = item.Kurz;
series.Points.Add(p1);
}
string filename = "D:\\Chart.png";
chart.SaveImage(filename, ChartImageFormat.Png);
Update: Setting the the Chart's Backcolor to Transparent actually works just fine. However some image viewers do not display transparency; I use Irfanview as my default viewer and it is one of those which can't. I suspect yours also misses transparency..
Instead all transparency is rendered as black, so unless you have a non-black line color.. your axes and labels etc.. seem to be gone. Paint (on W10) is another one but renders transparency to white, so the black pixels are at least visible.
The below image is from Photoshop, which, of course, doesn't have that problem..
Related
I can't find a way to change text color of a rectangle autoshape, please could I have help?
here code extract:
// Add shape
IAutoShape shape= slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 677, 250, 40);
((IAutoShape)shape).AddTextFrame( "Hello world!");
Default color is white, how can I change it?
The following code example shows how you can change the text color of a shape using Aspose.Slides for .NET:
var paragraph = shape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
You will get support and answers to your questions faster if you ask questions on the Aspose.Slides forum.
I work as a Support Developer at Aspose.
I've done this way and It works:
//In order to Have a no filled rectangle
((IAutoShape)shape).FillFormat.FillType = FillType.NoFill;
ITextFrame tf1 = ((IAutoShape)shape).TextFrame;
// Accessing the first Paragraph of validityDate
IParagraph para1 = tf1.Paragraphs[0];
// Accessing the first portion
IPortion port1 = para1.Portions[0];
// Define new fonts
FontData fd1 = new FontData("Arial");
// Assign new fonts to portion
port1.PortionFormat.LatinFont = fd1;
// Set font color
port1.PortionFormat.FillFormat.FillType = FillType.Solid;
port1.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
// Set the Height of the Font
port1.PortionFormat.FontHeight = 13;
I'm trying to fit image to button perfectly.
But the image is cropped on its right and bottom faces, see attached print screen:
I edited the button as follows:
var l_oStopImage = Image.FromFile(#"C:\Users\AmitL\Downloads\Button-2-stop-icon72p.png");
var l_oStopPic = new Bitmap(l_oStopImage , new Size(btnStopOperation.Width, btnStopOperation.Height));
btnStopOperation.Image = l_oStopPic ;
btnStopOperation.ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
btnStopOperation.TabStop = false;
btnStopOperation.FlatStyle = FlatStyle.Flat;
btnStopOperation.FlatAppearance.BorderSize = 0;
I also tried to edit the BackgroundImageLayout but none of the ImageLayouts fixed the problem..
Any suggestions?
Thanks in advance
1https://msdn.microsoft.com/en-us/library/system.windows.forms.imagelayout(v=vs.110).aspx
You should use stretch, I suggest in designtime (this is not java where you have to add elements by code):
this.buttonOk.BackColor = System.Drawing.SystemColors.MenuHighlight;
this.buttonOk.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonOk.BackgroundImage")));
this.buttonOk.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonOk.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonOk.Location = new System.Drawing.Point(475, 15);
this.buttonOk.Name = "buttonOk";
this.buttonOk.Size = new System.Drawing.Size(50, 50);
this.buttonOk.TabIndex = 11;
this.buttonOk.UseVisualStyleBackColor = false;
this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
And it will work, done it many times before
I got this code from my own working Form1.Designer.cs but because of that: please use the Visual Studio designer and don't try to write all this code / logic in your constructor or something.
The problem is because you are showing an image with the same size as your button.
When you want an image fit in your button, the width and height of image should be at least 1 point less than your button size. (or in other word, you can set your button width and height 1 point more than the image size).
So you can change your code to this:
var l_oStopPic = new Bitmap(l_oStopImage ,
new Size(btnStopOperation.Width-1, btnStopOperation.Height-1));
I have a list with checked checkboxes. All the items on the list is painted on a chart as a line. When I uncheck an Item, this item must be painted as a gray line. This do also happen, but NeXT item in the list gets the color of the item before it was grayed out. Can't really figure out why. Is it something in chart. Heres my code.
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
if (readyForChangingColor)
{
foreach (Series series in chart1.Series)
{
if (series.LegendText == e.Item.Text)
{
// if unchecked checkbox. Make the line gray
if (!e.Item.Checked)
{
series.Color = System.Drawing.Color.LightGray;
}
}
}
}
}
//Adding a serie to chart
var NewDataSeries = new System.Windows.Forms.DataVisualization.Charting.Series
{
Name = "SomeLogData" + Convert.ToString(NumberOfSets),
IsVisibleInLegend = true,
IsXValueIndexed = false,
ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line,
};
// Add the new series to the chart.
this.chart1.Series.Add(NewDataSeries);
The colors are assigned automatically according to a palette, unless a color has been set explicitly. So the next series down gets the freed up color.
To avoid this, you need to explicitly set the colors of all series.
See http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.series.palette(v=vs.110).aspx
What you probably want it to set the Color of a DataPoint, not of the whole Series.
To do so you simply set it:
yourDataPoint.Color = Color.Gray;
Note that this will set the color of the Point and (where applicable) the line coming to it from the previous Point. So the first Point's color will not show in a line segment..
Example of both Colors:
Series S = chart1.Series[0];
S.ChartType = SeriesChartType.Line;
S.Color = Color.Fuchsia;
S.Points.AddXY(1, 10); S.Points.AddXY(2, 20);
S.Points.AddXY(3, 60); S.Points.AddXY(4, 10);
DataPoint yourDataPoint = S.Points[2];
yourDataPoint.Color = Color.Gray;
I need to add a rectangle into my Visio file and want to set the font and text color,
how can I do this?
visio.Application app = new visio.Application();
visio.Document doc;
doc = app.Documents.Open(processPath);
visio.Page page = doc.Pages[1];
CreateVisio vis = new CreateVisio();
visio.Shape edit = page.DrawRectangle(3.2d, 6.9d, 4.9d, 7.9d);
How to Create a New VISIO Document
Microsoft.Office.Interop.Visio.Application application =
new Microsoft.Office.Interop.Visio.Application();
application.Visible = false;
Microsoft.Office.Interop.Visio.Document doc = application.Documents.Add(templatePath);
Microsoft.Office.Interop.Visio.Page page = application.Documents[1].Pages[1];
How to Get the Width and Height of the Sheet You are Working On
double xPosition = page.PageSheet.get_CellsU("PageWidth").ResultIU;
double yPosition = page.PageSheet.get_CellsU("PageHeight").ResultIU;
We are using this information about the sheet width and height to know where to place the boxes. We are putting the root boxes in the middle of the sheet by dividing the sheet width by the number of roots. Also we are subtracting from the yPosition the level number so that the boxes with increasing level number will get a lower position on the chart.
How to Create a Shape and Place it on the Chart (Drop it)
//creating the type of shape in the organizational chart it could be: "Position",
//"Executive", "Manager", "Assistant" and others according
//to what you have in your stencil.
Microsoft.Office.Interop.Visio.Master position = doc.Masters.get_ItemU("Position");
//placing the shape in the xPosition and yPosition coordinates
Microsoft.Office.Interop.Visio.Shape shape = page.Drop(position, xPosition, yPosition);
How to Set the Shapes Properties
//set the text
shape.Text = box.Name;
//set hyperlink
if (!String.IsNullOrEmpty(box.HyperLink.Trim()))
{
Hyperlink link = shape.Hyperlinks.Add();
link.Address = box.HyperLink;
}
//set the shape width
shape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.
visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.
visRowXFormIn,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.
visXFormWidth).ResultIU = box.Width;
//set the shape height
shape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.
visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.
visRowXFormIn,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.
visXFormHeight).ResultIU = box.Height;
//set the shape fore color
shape.Characters.set_CharProps(
(short)Microsoft.Office.Interop.Visio.
VisCellIndices.visCharacterColor,
(short)Utilities.GetVisioColor(box.ForeColor));
//set the shape back color
shape.get_CellsSRC((short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowFill,
(short)VisCellIndices.visFillForegnd).FormulaU =
"RGB(" + box.BackColor.R.ToString() + "," + box.BackColor.G.ToString() + ","
+ box.BackColor.B.ToString() + ")";
Connecting the shapes is done using the method connectWithDynamicGlueAndConnector(). This method accepts two parameters, the parent shape and the childShape and will create the connector between. This method is the exact one found in VISIO SDK.
I have written the following chunk of code that prints my ListBox perfectly when being sent to a physical printer, however when trying to send it to the XPS printer driver or using the XpsDocumentWriter class (I assume they use the same code under the hood) I receive the following exception:
System.ArgumentException was unhandled by user code
Message=Width and Height must be non-negative.
Source=ReachFramework
StackTrace:
at System.Windows.Xps.Serialization.VisualSerializer.WriteTileBrush(String element, TileBrush brush, Rect bounds)
The exception obviously points to an item not having a correct width/height however I have debugged the code when sending it to the different printers (physical and XPS driver) and I haven't been able to find any differences.
Below is how I create the visual to send to the printer:
private ScrollViewer GeneratePrintableView()
{
ScrollViewer scrollView = new ScrollViewer();
Grid grid = new Grid { Background = Brushes.White, Width = this.myListBox.ActualWidth, Height = this.myListBox.ActualHeight };
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions[0].Height = new GridLength(0, GridUnitType.Auto);
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Auto);
// Add the title and icon to the top
VisualBrush titleClone = new VisualBrush(this.TitleBar);
var titleRectangle = new Rectangle { Fill = titleClone, Width = this.TitleBar.ActualWidth, Height = this.TitleBar.ActualHeight };
grid.Children.Add(titleRectangle);
Grid.SetRow(titleRectangle, 0);
this.myListBox.Width = this.myListBox.ActualWidth;
this.myListBox.Height = this.myListBox.ActualHeight;
VisualBrush clone = new VisualBrush(this.myListBox) { Stretch = Stretch.None, AutoLayoutContent = true };
var rectangle = new Rectangle { Fill = clone, Width = this.myListBox.ActualWidth, Height = this.myListBox.ActualHeight };
Border border = new Border { Background = Brushes.White, Width = this.myListBox.ActualWidth, Height = this.myListBox.ActualHeight };
border.Child = rectangle;
grid.Children.Add(border);
Grid.SetRow(border, 1);
scrollView.Width = this.myListBox.ActualWidth;
scrollView.Height = this.myListBox.ActualHeight;
scrollView.Content = grid;
scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
return scrollView;
}
Here is the GetPage override in my DocumentPaginator implementation:
public override DocumentPage GetPage(int pageNumber)
{
Page page = new Page();
double z = 0.0;
this.grid = new Grid();
this.grid.RowDefinitions.Add(new RowDefinition());
this.grid.RowDefinitions[0].Height = new GridLength(0, GridUnitType.Auto);
this.grid.Children.Add(this.printViewer);
Grid.SetRow(this.printViewer, 0);
//Adjusting the vertical scroll offset depending on the page number
if (pageNumber + 1 == 1) //if First Page
{
this.printViewer.ScrollToVerticalOffset(0);
this.printViewer.UpdateLayout();
}
else if (pageNumber + 1 == _verticalPageCount) //if Last Page
{
if (this.printViewer.ScrollableHeight == 0) //If printing only single page and the contents fits only on one page
{
this.printViewer.ScrollToVerticalOffset(0);
this.printViewer.UpdateLayout();
}
else if (this.printViewer.ScrollableHeight <= this.printViewer.Height) //If scrollconentheight is less or equal than scrollheight
{
this.printViewer.Height = this.printViewer.ScrollableHeight;
this.printViewer.ScrollToEnd();
this.printViewer.UpdateLayout();
}
else //if the scrollcontentheight is greater than scrollheight then set the scrollviewer height to be the remainder between scrollcontentheight and scrollheight
{
this.printViewer.Height = (this.printViewer.ScrollableHeight % this.printViewer.Height) + 5;
this.printViewer.ScrollToEnd();
this.printViewer.UpdateLayout();
}
}
else //Other Pages
{
z = z + this.printViewer.Height;
this.printViewer.ScrollToVerticalOffset(z);
this.printViewer.UpdateLayout();
}
page.Content = this.grid; //put the grid into the page
page.Arrange(new Rect(this.originalMargin.Left, this.originalMargin.Top, this.ContentSize.Width, this.ContentSize.Height));
page.UpdateLayout();
return new DocumentPage(page);
}
Interestingly if I change the Fill of rectangle to a Brush instead of clone then I do not receive the exception and the outputted file is the correct size.
I have spent over a day trying to debug why this isn't working and I am hoping that someone out there has either seen a similar issue or is able to point out any mistakes I am making.
Thanks for any responses.
I had to give up finding a solution with VisualBrush. If there is a GroupBox in the Visual for the brush I could never get it to produce a XPS file. It always fails with
System.ArgumentException was unhandled by user code Message=Width and Height must be non-negative. Source=ReachFramework StackTrace: at System.Windows.Xps.Serialization.VisualSerializer.WriteTileBrush(String element, TileBrush brush, Rect bounds)
The workaround was to clone the content that should go in the VisualBrush (Is there an easy/built-in way to get an exact copy (clone) of a XAML element?) and use that directly in a Grid instead of an VisualBrush
Have you checked the value of ActualWidth and ActualHeight of myListBox when the VisualBrush is being created? I don't know from where myListBox comes, but if it is not rendered by the time you are generating your xps document you may run into problems. You can try to manually force the control to render and see if it makes any difference.
I was unable to rectify the problem however using this link Paginated printing of WPF visuals I was able to find a suitable solution to allow printing of complicated visuals within my WPF application.
It's 2016 now and it's still not fixed. The problem is using TileBrush or any descendant type (VisualBrush in your case). If you use absolute mapping, it works, it's the relative mapping that causes the problem. Calculate the final size yourself and set Viewport to this size, ViewportUnits to Absolute. Also make sure you don't use Stretch.