Skip to content

Commit

Permalink
Merge pull request #166 from startewho/areachat
Browse files Browse the repository at this point in the history
Add Areachat
  • Loading branch information
PrzemyslawKlys authored Oct 21, 2023
2 parents 7dac5a3 + 75e3cdc commit c8a85f7
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 87 deletions.
10 changes: 10 additions & 0 deletions OfficeIMO.Examples/Word/Charts/Charts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ public static void Example_AddingMultipleCharts(string folderPath, bool openWord
barChart3.BarGrouping = BarGroupingValues.Clustered;
barChart3.BarDirection = BarDirectionValues.Column;

var areaChart = document.AddAreaChart("AreaChart");
areaChart.AddCategories(categories);

areaChart.AddChartArea("Brazil", new List<int>() { 100, 1, 18, 230 }, SixLabors.ImageSharp.Color.Brown);
areaChart.AddChartArea("Poland", new List<int>() { 13, 20, 230, 150 }, SixLabors.ImageSharp.Color.Green);
areaChart.AddChartArea("USA", new List<int>() { 10, 305, 18, 23 }, SixLabors.ImageSharp.Color.AliceBlue);

areaChart.AddLegend(LegendPositionValues.Top);


Console.WriteLine("Charts count: " + document.Sections[0].Charts.Count);

Console.WriteLine("Images count: " + document.Sections[0].Images.Count);
Expand Down
20 changes: 18 additions & 2 deletions OfficeIMO.Tests/Word.Charts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,30 @@ public void Test_BasicWordWithCharts() {
Assert.True(document.Charts.Count == 4);
Assert.True(document.ParagraphsCharts.Count == 4);

var areaChart = document.AddAreaChart("AreaChart");
areaChart.AddCategories(categories);

areaChart.AddChartArea("USA", new List<int>() { 10, 35, 18, 23 }, SixLabors.ImageSharp.Color.Brown);
areaChart.AddChartArea("Brazil", new List<int>() { 10, 35, 300, 13 }, SixLabors.ImageSharp.Color.Green);
areaChart.AddChartArea("Poland", new List<int>() { 10, 35, 230, 150 }, SixLabors.ImageSharp.Color.AliceBlue);

areaChart.AddLegend(LegendPositionValues.Top);

Assert.True(document.Sections[0].ParagraphsCharts.Count == 3);
Assert.True(document.Sections[0].Charts.Count == 3);
Assert.True(document.Sections[1].Charts.Count == 2);
Assert.True(document.Sections[1].ParagraphsCharts.Count == 2);
Assert.True(document.Charts.Count == 5);
Assert.True(document.ParagraphsCharts.Count == 5);

document.Save(false);
}

using (WordDocument document = WordDocument.Load(filePath)) {

Assert.True(document.Sections[0].Charts.Count == 3);
Assert.True(document.Sections[1].Charts.Count == 1);
Assert.True(document.Charts.Count == 4);
Assert.True(document.Sections[1].Charts.Count == 2);
Assert.True(document.Charts.Count == 5);

document.Save(false);
}
Expand Down
121 changes: 121 additions & 0 deletions OfficeIMO.Word/WordAreaChart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Collections.Generic;
using System.Linq;

namespace OfficeIMO.Word {
public class WordAreaChart : WordChart {

public static WordChart AddAreaChart(WordDocument wordDocument, WordParagraph paragraph,string title=null, bool roundedCorners = false, int width = 600, int height = 600) {
_document = wordDocument;
_paragraph = paragraph;

// minimum required to create chart
var oChart = GenerateChart(title);
oChart = GenerateAreaChart(oChart);

// inserts chart into document
InsertChart(wordDocument, paragraph, oChart, roundedCorners, width, height);

var drawing = paragraph._paragraph.OfType<Drawing>().FirstOrDefault();

return new WordChart(_document, _paragraph._paragraph, drawing);
}

internal static AreaChart CreateAreaChart() {
AreaChart chart = new AreaChart();
chart.AddNamespaceDeclaration("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");
Grouping grouping1 = new Grouping() { Val = GroupingValues.Standard };

DataLabels dataLabels1 = AddDataLabel();
chart.Append(dataLabels1);

chart.Append(grouping1);

AxisId axisId1 = new AxisId() { Val = (UInt32Value)148921728U };
axisId1.AddNamespaceDeclaration("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");

AxisId axisId2 = new AxisId() { Val = (UInt32Value)154227840U };
axisId2.AddNamespaceDeclaration("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");

chart.Append(axisId1);
chart.Append(axisId2);
return chart;
}

private static Chart GenerateAreaChart(Chart chart) {
AreaChart areaChart = CreateAreaChart();

CategoryAxis categoryAxis1 = AddCategoryAxis();
ValueAxis valueAxis1 = AddValueAxis();

//chart.PlotArea.Append(layout1);
chart.PlotArea.Append(categoryAxis1);
chart.PlotArea.Append(valueAxis1);
chart.PlotArea.Append(areaChart);


return chart;
}

internal static AreaChartSeries AddAreaChartSeries<T>(UInt32Value index, string series, SixLabors.ImageSharp.Color color, List<string> categories, List<T> data) {
AreaChartSeries lineChartSeries1 = new AreaChartSeries();
DocumentFormat.OpenXml.Drawing.Charts.Index index1 = new DocumentFormat.OpenXml.Drawing.Charts.Index() { Val = index };
Order order1 = new Order() { Val = index };

SeriesText seriesText1 = new SeriesText();


NumericValue numericValue1 = new NumericValue();
numericValue1.Text = series;

seriesText1.Append(numericValue1);


var chartShapeProperties1 = AddShapeProperties(color);

Values values1 = AddValuesAxisData(data);
CategoryAxisData categoryAxisData1 = AddCategoryAxisData(categories);


lineChartSeries1.Append(index1);
lineChartSeries1.Append(order1);
lineChartSeries1.Append(seriesText1);
lineChartSeries1.Append(chartShapeProperties1);

lineChartSeries1.Append(categoryAxisData1);
lineChartSeries1.Append(values1);

return lineChartSeries1;


}

internal static ChartShapeProperties AddShapeProperties(SixLabors.ImageSharp.Color color) {
ChartShapeProperties chartShapeProperties1 = new ChartShapeProperties();

DocumentFormat.OpenXml.Drawing.Outline outline1 = new DocumentFormat.OpenXml.Drawing.Outline();

DocumentFormat.OpenXml.Drawing.SolidFill solidFill1 = new DocumentFormat.OpenXml.Drawing.SolidFill();
DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColorModelHex1 = new DocumentFormat.OpenXml.Drawing.RgbColorModelHex() { Val = color.ToHexColor() };




solidFill1.Append(rgbColorModelHex1);

outline1.Append(solidFill1);

chartShapeProperties1.Append(solidFill1.CloneNode(true));
chartShapeProperties1.Append(outline1);

return chartShapeProperties1;
}



public WordAreaChart(WordDocument document, Paragraph paragraph, Drawing drawing) : base(document, paragraph, drawing) {
}
}
}
8 changes: 4 additions & 4 deletions OfficeIMO.Word/WordBarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

namespace OfficeIMO.Word {
public class WordBarChart : WordChart {
public static WordChart AddBarChart(WordDocument wordDocument, WordParagraph paragraph, bool roundedCorners = false) {
public static WordChart AddBarChart(WordDocument wordDocument, WordParagraph paragraph, string title = null, bool roundedCorners = false, int width = 600, int height = 600) {
_document = wordDocument;
_paragraph = paragraph;

// minimum required to create chart
var oChart = GenerateChart();
var oChart = GenerateChart(title);
oChart = GenerateChartBar(oChart);

InsertChart(wordDocument, paragraph, oChart, roundedCorners);
InsertChart(wordDocument, paragraph, oChart, roundedCorners, width, height);

var drawing = paragraph._paragraph.OfType<Drawing>().FirstOrDefault();

Expand Down Expand Up @@ -70,7 +70,7 @@ internal static ChartShapeProperties AddShapeProperties(SixLabors.ImageSharp.Col

}

internal static BarChartSeries AddBarChartSeries(UInt32Value index, string series, SixLabors.ImageSharp.Color color, List<string> categories, List<int> data) {
internal static BarChartSeries AddBarChartSeries<T>(UInt32Value index, string series, SixLabors.ImageSharp.Color color, List<string> categories, List<T> data) {
BarChartSeries barChartSeries1 = new BarChartSeries();

DocumentFormat.OpenXml.Drawing.Charts.Index index1 = new DocumentFormat.OpenXml.Drawing.Charts.Index() { Val = index };
Expand Down
73 changes: 60 additions & 13 deletions OfficeIMO.Word/WordChart.PublicMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -11,32 +11,44 @@ public void AddCategories(List<string> categories) {
Categories = categories;
}



public void AddChartPie(string name, int value) {



public void AddChartPie<T>(string name, int[] values) {
if (_chart != null) {
var pieChart = _chart.PlotArea.GetFirstChild<PieChart>();
if (pieChart != null) {
PieChartSeries pieChartSeries = WordPieChart.AddPieChartSeries(this._index, name, this.Categories, values.ToList());
pieChart.Append(pieChartSeries);
}
}
}

public void AddChartPie(string name, List<int> values) {
public void AddChartPie<T>(string name, List<T> values) {
if (_chart != null) {
var pieChart = _chart.PlotArea.GetFirstChild<PieChart>();
if (pieChart != null) {
PieChartSeries pieChartSeries = WordPieChart.AddPieChartSeries(this._index, name, Color.AliceBlue, this.Categories, values);
PieChartSeries pieChartSeries = WordPieChart.AddPieChartSeries(this._index, name, this.Categories, values);
pieChart.Append(pieChartSeries);
}
}
}

public void AddChartLine<T>(string name, int[] values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var lineChart = _chart.PlotArea.GetFirstChild<LineChart>();
if (lineChart != null) {
LineChartSeries lineChartSeries = WordLineChart.AddLineChartSeries(this._index, name, color, this.Categories, values.ToList());
lineChart.Append(lineChartSeries);
}
}
}

/// <summary>
/// Add a line to a chart. Multiple lines can be added.
/// You cannot mix lines with pies or bars.
/// </summary>
/// <param name="name"></param>
/// <param name="values"></param>
/// <param name="color"></param>
public void AddChartLine(string name, List<int> values, Color color) {
public void AddChartLine<T>(string name, List<T> values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var lineChart = _chart.PlotArea.GetFirstChild<LineChart>();
if (lineChart != null) {
Expand All @@ -50,7 +62,7 @@ public void AddChartAxisX(List<string> categories) {
Categories = categories;
}

public void AddChartBar(string name, int values, Color color) {
public void AddChartBar(string name, int values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var barChart = _chart.PlotArea.GetFirstChild<BarChart>();
if (barChart != null) {
Expand All @@ -59,7 +71,7 @@ public void AddChartBar(string name, int values, Color color) {
}
}
}
public void AddChartBar(string name, List<int> values, Color color) {
public void AddChartBar<T>(string name, List<T> values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var barChart = _chart.PlotArea.GetFirstChild<BarChart>();
if (barChart != null) {
Expand All @@ -68,7 +80,8 @@ public void AddChartBar(string name, List<int> values, Color color) {
}
}
}
public void AddChartBar(string name, int[] values, Color color) {

public void AddChartBar(string name, int[] values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var barChart = _chart.PlotArea.GetFirstChild<BarChart>();
if (barChart != null) {
Expand All @@ -77,5 +90,39 @@ public void AddChartBar(string name, int[] values, Color color) {
}
}
}

public void AddChartArea<T>(string name, List<T> values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var barChart = _chart.PlotArea.GetFirstChild<AreaChart>();
if (barChart != null) {
AreaChartSeries areaChartSeries = WordAreaChart.AddAreaChartSeries(this._index, name, color, this.Categories, values);
barChart.Append(areaChartSeries);
}
}
}

public void AddChartArea<T>(string name, int[] values, SixLabors.ImageSharp.Color color) {
if (_chart != null) {
var barChart = _chart.PlotArea.GetFirstChild<AreaChart>();
if (barChart != null) {
AreaChartSeries areaChartSeries = WordAreaChart.AddAreaChartSeries(this._index, name, color, this.Categories, values.ToList());
barChart.Append(areaChartSeries);
}
}
}

public void AddLegend(LegendPositionValues legendPosition) {
if (_chart != null) {

Legend legend = new Legend();
LegendPosition postion = new LegendPosition() { Val = legendPosition };
Overlay overlay = new Overlay() { Val = false };
legend.Append(postion);
legend.Append(overlay);
_chart.Append(legend);

}
}

}
}
Loading

0 comments on commit c8a85f7

Please sign in to comment.