using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using Aspose.Cells.Charts;
using Aspose.Cells.Drawing;
using System.Drawing;
using System.IO;
namespace AsposeProject
{
class Charts
{
public static void Createchart1()
{
//License l = new License();
//l.SetLicense(“Aspose.Total.lic”);
string filePath = @“F:\Shak-Data-RW\Downloads\test”;
Workbook wb = new Workbook(filePath + “example.xlsx”);
Worksheet wsChart = wb.Worksheets[0];
Worksheet wsData = wb.Worksheets[1];
wsData.IsVisible = false;
//Erzeugen des Diagramms und setzen der wesentlichen Eigenschaften
int chartIndex = wsChart.Charts.Add(ChartType.Line, 23, 0, 62, 84);
Chart chart = wsChart.Charts[chartIndex];
//Hauptgridzeielen einblenden
//chart. MajorGridLines.IsVisible = true;
//Grundfarbe weiß
chart.PlotArea.Area.ForegroundColor = Color.White;
//Kein umschließender Rahmen
chart.ChartArea.Border.IsVisible = false;
//Festlegung der Datenquelle
chart.NSeries.Add("’" + wsData.Name + “’!D2:D” + Convert.ToString(216), true);
chart.NSeries.Add("’" + wsData.Name + “’!C2:C” + Convert.ToString(212), true);
chart.NSeries.Add("’" + wsData.Name + “’!F2:F” + Convert.ToString(216), true);
chart.NSeries.Add("’" + wsData.Name + “’!E2:E” + Convert.ToString(212), true);
chart.NSeries.CategoryData = “’” + wsData.Name + “’!B2:B” + Convert.ToString(216);
//Anpassen der Farben der einzelnen Datenreihen
chart.NSeries[1].Line.Color = Color.DarkBlue;
chart.NSeries[1].Line.Weight = WeightType.WideLine;
chart.NSeries[0].Line.Color = Color.Yellow;
chart.NSeries[0].Line.Weight = WeightType.WideLine;
chart.NSeries[3].Line.Color = Color.Red;
chart.NSeries[3].Line.Weight = WeightType.WideLine;
chart.NSeries[2].Line.Color = Color.Orange;
chart.NSeries[2].Line.Weight = WeightType.WideLine;
//Legende Ausblenden
chart.ShowLegend = false;
chart.ValueAxis.MinValue = 0;
chart.ValueAxis.MaxValue = 1;
chart.ValueAxis.MajorUnit = 0.1;
chart.ValueAxis.TickLabels.NumberFormat = “0%”;
//Formatierung der Label der X-Achse
chart.CategoryAxis.TickLabels.Font.Size = 8;
chart.CategoryAxis.AxisBetweenCategories = false;
chart.CategoryAxis.TickLabelSpacing = 5;
chart.CategoryAxis.TickLabels.RotationAngle = 0;
//chart.Calculate();
//Neuausrichten der Plotarea
chart.PlotArea.Height -= 450;
chart.PlotArea.Y -= 550;
chart.Calculate();
//Wasserzeichen / Quelllabel einstellen
FileStream stream = new FileStream(filePath + “logochart.bmp”, FileMode.Open, FileAccess.Read);
Picture pic0 = chart.Shapes.AddPictureInChart(chart.PlotAreaWithoutTickLabels.Y + 25, chart.PlotAreaWithoutTickLabels.X + 15, stream, 90, 90);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 0, “01-2008”, 0);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 5, “06-2008”, 190);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 10, “11-2008”, 0);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 15, “16-2008”, 190);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 20, “21-2008”, 0);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 25, “26-2008”, 190);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 30, “31-2008”, 0);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 35, “36-2008”, 190);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 40, “41-2008”, 0);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 45, “46-2008”, 190);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 50, “51-2008”, 0);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 55, “04-2009”, 190);
//AddTextbox(chart, Color.Black, Color.LightGray, 300, 60, “09-2009”, 0);
wb.Save(filePath + “reportAspose.xlsx”, SaveFormat.Xlsx);
wb.Save(filePath+ “reportAspose.pdf”, SaveFormat.Pdf);
//System.Diagnostics.Process.Start(Path.Combine(System.Windows.Forms.Application.StartupPath, “reportAspose.pdf”));
}
private void AddTextbox(Chart chart, Color line, Color background, int widthbox, int index, string text, int top)
{
int tickSpacing = chart.CategoryAxis.TickLabelSpacing;
int dataCount = chart.NSeries[0].CountOfDataValues;
if (!chart.CategoryAxis.AxisBetweenCategories)
{
dataCount -= 1;
}
int x = chart.PlotAreaWithoutTickLabels.X;
int y = chart.PlotAreaWithoutTickLabels.Y;
int width = chart.PlotAreaWithoutTickLabels.Width;
int height = chart.PlotAreaWithoutTickLabels.Height;
int unit = (int)(width / (dataCount));
//Erst die Linie, damit diese später durch die Textbox überlagert werden kann
Shape s = chart.Shapes.AddShapeInChart(MsoDrawingType.Line, PlacementType.MoveAndSize, x + (int)(unit * index) + (int)(index), top + 80, x + (int)(unit * (index)), 4000 - (4000 - y - height + 80));
s.LineFormat.ForeColor = line;
//Jetzt die Textbox integrieren
TextBox t = chart.Shapes.AddTextBoxInChart(top, x + (int)(unit * (index)), 150, widthbox);
t.LineFormat.ForeColor = line;
t.FillFormat.ForeColor = background;
t.Text = text;
t.Font.Size = 6;
t.TextHorizontalAlignment = TextAlignmentType.Center;
t.TextVerticalAlignment = TextAlignmentType.Center;
}
}
}