CPF/CPF.ReoGrid/Chart/ChartLegend.cs

180 lines
4.3 KiB
C#
Raw Normal View History

2024-06-24 10:15:59 +08:00
using System;
using CPF.Drawing;
using CPF.ReoGrid.Common;
using CPF.ReoGrid.Drawing;
using CPF.ReoGrid.Rendering;
namespace CPF.ReoGrid.Chart
{
public class ChartLegend : DrawingComponent
{
public virtual IChart Chart { get; protected set; }
public ChartLegend(IChart chart)
{
this.Chart = chart;
base.LineColor = Color.Transparent;
base.FillColor = Color.Transparent;
this.FontSize *= 0.8f;
}
public LegendType LegendType { get; set; }
public LegendPosition LegendPosition
{
get
{
return this.legendPosition;
}
set
{
bool flag = this.legendPosition != value;
if (flag)
{
this.legendPosition = value;
bool flag2 = this.Chart is Chart;
if (flag2)
{
Chart chart = (Chart)this.Chart;
chart.DirtyLayout();
}
}
}
}
protected virtual Size GetSymbolSize(int index)
{
float num = 14f;
float num2 = 14f;
return new Size(ref num, ref num2);
}
protected virtual Size GetLabelSize(int index)
{
WorksheetChartDataSource dataSource = this.Chart.DataSource;
bool flag = dataSource == null;
Size result;
if (flag)
{
result = default(Size);
}
else
{
string label = dataSource[index].Label;
result = DrawingFactory.Default.MeasureString(label, ResourcePoolManager.CreateFont(this.FontName, (double)this.FontSize, PlatformUtility.ToWPFFontStyle(this.FontStyles)));
}
return result;
}
public override Size GetPreferredSize()
{
return this.layoutedSize;
}
public virtual void MeasureSize(Rect parentClientRect)
{
WorksheetChartDataSource dataSource = this.Chart.DataSource;
bool flag = dataSource == null;
if (!flag)
{
int serialCount = dataSource.SerialCount;
this.Children.Clear();
float num = 0f;
float num2 = 0f;
float num3 = 0f;
float num4 = 0f;
float num5;
for (int i = 0; i < serialCount; i++)
{
ChartLegendItem chartLegendItem = new ChartLegendItem(this, i);
Size symbolSize = this.GetSymbolSize(i);
bool flag2 = num < symbolSize.Width;
if (flag2)
{
num = symbolSize.Width;
}
bool flag3 = num2 < symbolSize.Height;
if (flag3)
{
num2 = symbolSize.Height;
}
ChartLegendItem chartLegendItem2 = chartLegendItem;
num5 = 0f;
float num6 = 0f;
Point point = new Point(ref num5, ref num6);
chartLegendItem2.SymbolBounds = new Rect(ref point, ref symbolSize);
Size labelSize = this.GetLabelSize(i);
labelSize.Width += 6f;
bool flag4 = num3 < labelSize.Width;
if (flag4)
{
num3 = labelSize.Width;
}
bool flag5 = num4 < labelSize.Height;
if (flag5)
{
num4 = labelSize.Height;
}
ChartLegendItem chartLegendItem3 = chartLegendItem;
num5 = 0f;
num6 = 0f;
point = new Point(ref num5, ref num6);
chartLegendItem3.LabelBounds = new Rect(ref point, ref labelSize);
this.Children.Add(chartLegendItem);
}
float num7 = num + 4f + num3;
float num8 = Math.Max(num2, num4);
Rect rect = parentClientRect;
float num9 = 0f;
float num10 = 0f;
float num11 = 0f;
float num12 = 0f;
for (int j = 0; j < serialCount; j++)
{
ChartLegendItem chartLegendItem4 = this.Children[j] as ChartLegendItem;
bool flag6 = chartLegendItem4 != null;
if (flag6)
{
chartLegendItem4.SetSymbolLocation(0f, (num8 - chartLegendItem4.SymbolBounds.Height) / 2f);
chartLegendItem4.SetLabelLocation(num + 4f, (num8 - chartLegendItem4.LabelBounds.Height) / 2f);
chartLegendItem4.Bounds = new Rect(ref num9, ref num10, ref num7, ref num8);
bool flag7 = num11 < chartLegendItem4.Right;
if (flag7)
{
num11 = chartLegendItem4.Right;
}
bool flag8 = num12 < chartLegendItem4.Bottom;
if (flag8)
{
num12 = chartLegendItem4.Bottom;
}
}
num9 += num7;
bool flag9 = this.LegendPosition == LegendPosition.Left || this.LegendPosition == LegendPosition.Right;
if (flag9)
{
num9 = 0f;
num10 += num8 + 10f;
}
else
{
num9 += 10f;
bool flag10 = num9 > rect.Width;
if (flag10)
{
num9 = 0f;
num10 += num8 + 10f;
}
}
}
num5 = num11 + 10f;
this.layoutedSize = new Size(ref num5, ref num12);
}
}
private LegendPosition legendPosition;
private Size layoutedSize;
}
}