如何将 3D TeeChart 中图例框的颜色设置为底层数据系列的颜色?
How to set the color of the legendbox in 3D TeeChart to that of the underlying data series?
我正在使用 Teechart 在 winforms 中创建 3D 图。当我将第一个表面添加到图表中时,图例框显示图例项(用于值),框内填充有颜色。但是,当我添加多个表面时,图例中的图例项框(代表表面)为白色。我希望它们充满 surface/series 颜色。
我已经通过设置
设置了图例项文本的颜色
tChart.Legend.FontSeriesColor
正确,但我不知道如何用颜色填充图例框。
我附上了两种场景的屏幕截图 -
One surface。
Two surfaces
谢谢大家
示例代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DisplayChart();
}
private void DisplayChart()
{
tChart1.Series.Clear();
tChart1.Legend.FontSeriesColor = true;
CreateSurface(new Plot3DSurface("X", "Y", "Z", new List<double> { 1.0, 2.0, 3.0 }, new List<double> { 3, 4, 7 }, new List<double> { 3, 5, 7 }, GetPoints(3, 3), "First", Color.Blue));
CreateSurface(new Plot3DSurface("X", "Y", "Z", new List<double> { 1.0, 3.0, 6.0 }, new List<double> { 3, 5, 8 }, new List<double> { 5, 8, 10.5 }, GetPoints(3, 3), "Second", Color.Green));
}
private static List<Point> GetPoints(int x, int y)
{
var points = new List<Point>();
for (var p = 0; p < x; p++)
{
for (var t = 0; t < y; t++)
{
points.Add(new Point(p, t));
}
}
return points;
}
private void CreateSurface(IPlot3DSurface surfaceInfo)
{
Custom3D surface;
var xCount = surfaceInfo.IndexesUsed.Select(index => index.X).Distinct().Count();
var yCount = surfaceInfo.IndexesUsed.Select(index => index.Y).Distinct().Count();
if (xCount < 2 || yCount < 2)
{
surface = new Points3D(tChart1.Chart);
}
else
{
surface = new Surface(tChart1.Chart)
{
Title = surfaceInfo.Title,
WireFrame = false,
DotFrame = false,
UseColorRange = true,
UsePalette = false,
ColorEach = false,
IrregularGrid = true,
PaletteStyle = PaletteStyles.Strong,
StartColor = surfaceInfo.Color,
Color = surfaceInfo.Color,
};
}
DrawPoints(surfaceInfo, surface);
}
private void DrawPoints(IPlot3DSurface surfaceInfo, Custom3D surface)
{
foreach (var point in surfaceInfo.IndexesUsed)
{
surface.Add(surfaceInfo.GetXValue(point.X), surfaceInfo.GetZValue(point.X), surfaceInfo.GetYValue(point.Y));
}
}
}
public class Plot3DSurface : IPlot3DSurface
{
public Plot3DSurface(string xCoordinatePropertyName, string yCoordinatePropertyName, string zCoordinatePropertyName,
List<double> dataSourceListXCoordinate, List<double> dataSourceListYCoordinate, List<double> dataSourceZCoordinate,
List<Point> dataSourceIndexList, string title, Color color)
{
DataSourceListXCoordinate = dataSourceListXCoordinate;
DataSourceListYCoordinate = dataSourceListYCoordinate;
DataSourceListZCoordinate = dataSourceZCoordinate;
XCoordinatePropertyName = xCoordinatePropertyName;
YCoordinatePropertyName = yCoordinatePropertyName;
ZCoordinatePropertyName = zCoordinatePropertyName;
Title = title;
Color = color;
IndexesUsed = dataSourceIndexList;
}
private List<Point> _indexesUsed;
public List<Point> IndexesUsed
{
get
{
if (_indexesUsed != null) return _indexesUsed;
var ind = new List<Point>();
for (var i = 0; i < DataCountXCoordinate; i++)
for (var j = 0; j < DataCountYCoordinate; j++)
ind.Add(new Point(i, j));
return ind;
}
private set { _indexesUsed = value; }
}
private IList<double> DataSourceListXCoordinate { get; set; }
private IList<double> DataSourceListYCoordinate { get; set; }
private IList<double> DataSourceListZCoordinate { get; set; }
private int DataCountXCoordinate
{
get { return DataSourceListXCoordinate.Count; }
}
private int DataCountYCoordinate
{
get { return DataSourceListYCoordinate.Count; }
}
private string XCoordinatePropertyName { get; set; }
private string YCoordinatePropertyName { get; set; }
private string ZCoordinatePropertyName { get; set; }
public string XCoordinatePropertyDisplayName
{
get { return XCoordinatePropertyName + " (" + DataSourceListXCoordinate[0] + ")"; }
}
public string YCoordinatePropertyDisplayName
{
get { return YCoordinatePropertyName + " (" + DataSourceListYCoordinate[0] + ")"; }
}
public string ZCoordinatePropertyDisplayName
{
get { return ZCoordinatePropertyName + " (" + DataSourceListZCoordinate[0] + ")"; }
}
public string Title { get; private set; }
public Color Color { get; private set; }
public double? GetXValue(int i)
{
return DataSourceListXCoordinate[i];
}
public double? GetYValue(int i)
{
return DataSourceListYCoordinate[i];
}
public double? GetZValue(int i)
{
return DataSourceListZCoordinate[i];
}
public double GetMinX
{
get { return DataSourceListXCoordinate.Min(v => v); }
}
public double GetMaxX
{
get { return DataSourceListXCoordinate.Max(v => v); }
}
public double GetMinY
{
get { return DataSourceListYCoordinate.Min(v => v); }
}
public double GetMaxY
{
get { return DataSourceListYCoordinate.Max(v => v); }
}
public double GetMinZ
{
get { return DataSourceListZCoordinate.Min(v => v); }
}
public double GetMaxZ
{
get { return DataSourceListZCoordinate.Max(v => v); }
}
}
public interface IPlot3DSurface
{
List<Point> IndexesUsed { get; }
string XCoordinatePropertyDisplayName { get; }
string YCoordinatePropertyDisplayName { get; }
string ZCoordinatePropertyDisplayName { get; }
string Title { get; }
double? GetXValue(int i);
double? GetYValue(int i);
double? GetZValue(int i);
double GetMinX { get; }
double GetMaxX { get; }
double GetMinY { get; }
double GetMaxY { get; }
double GetMinZ { get; }
double GetMaxZ { get; }
Color Color { get; }
}
Surface
系列在使用ColorRange绘制单元格时,不使用笔刷绘制图例符号。
如果你想强制在图例符号处绘制系列颜色,你可以使用 OnSymbolDraw
事件:
tChart1.Legend.Symbol.OnSymbolDraw += Symbol_OnSymbolDraw;
并在其中手动绘制符号:
private void Symbol_OnSymbolDraw(object sender, SymbolDrawEventArgs e)
{
if (e.Series != null)
{
tChart1.Graphics3D.Brush.Color = e.Series.Color;
}
tChart1.Graphics3D.Rectangle(e.Rect);
}
我正在使用 Teechart 在 winforms 中创建 3D 图。当我将第一个表面添加到图表中时,图例框显示图例项(用于值),框内填充有颜色。但是,当我添加多个表面时,图例中的图例项框(代表表面)为白色。我希望它们充满 surface/series 颜色。
我已经通过设置
设置了图例项文本的颜色tChart.Legend.FontSeriesColor
正确,但我不知道如何用颜色填充图例框。
我附上了两种场景的屏幕截图 -
One surface。 Two surfaces
谢谢大家
示例代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DisplayChart();
}
private void DisplayChart()
{
tChart1.Series.Clear();
tChart1.Legend.FontSeriesColor = true;
CreateSurface(new Plot3DSurface("X", "Y", "Z", new List<double> { 1.0, 2.0, 3.0 }, new List<double> { 3, 4, 7 }, new List<double> { 3, 5, 7 }, GetPoints(3, 3), "First", Color.Blue));
CreateSurface(new Plot3DSurface("X", "Y", "Z", new List<double> { 1.0, 3.0, 6.0 }, new List<double> { 3, 5, 8 }, new List<double> { 5, 8, 10.5 }, GetPoints(3, 3), "Second", Color.Green));
}
private static List<Point> GetPoints(int x, int y)
{
var points = new List<Point>();
for (var p = 0; p < x; p++)
{
for (var t = 0; t < y; t++)
{
points.Add(new Point(p, t));
}
}
return points;
}
private void CreateSurface(IPlot3DSurface surfaceInfo)
{
Custom3D surface;
var xCount = surfaceInfo.IndexesUsed.Select(index => index.X).Distinct().Count();
var yCount = surfaceInfo.IndexesUsed.Select(index => index.Y).Distinct().Count();
if (xCount < 2 || yCount < 2)
{
surface = new Points3D(tChart1.Chart);
}
else
{
surface = new Surface(tChart1.Chart)
{
Title = surfaceInfo.Title,
WireFrame = false,
DotFrame = false,
UseColorRange = true,
UsePalette = false,
ColorEach = false,
IrregularGrid = true,
PaletteStyle = PaletteStyles.Strong,
StartColor = surfaceInfo.Color,
Color = surfaceInfo.Color,
};
}
DrawPoints(surfaceInfo, surface);
}
private void DrawPoints(IPlot3DSurface surfaceInfo, Custom3D surface)
{
foreach (var point in surfaceInfo.IndexesUsed)
{
surface.Add(surfaceInfo.GetXValue(point.X), surfaceInfo.GetZValue(point.X), surfaceInfo.GetYValue(point.Y));
}
}
}
public class Plot3DSurface : IPlot3DSurface
{
public Plot3DSurface(string xCoordinatePropertyName, string yCoordinatePropertyName, string zCoordinatePropertyName,
List<double> dataSourceListXCoordinate, List<double> dataSourceListYCoordinate, List<double> dataSourceZCoordinate,
List<Point> dataSourceIndexList, string title, Color color)
{
DataSourceListXCoordinate = dataSourceListXCoordinate;
DataSourceListYCoordinate = dataSourceListYCoordinate;
DataSourceListZCoordinate = dataSourceZCoordinate;
XCoordinatePropertyName = xCoordinatePropertyName;
YCoordinatePropertyName = yCoordinatePropertyName;
ZCoordinatePropertyName = zCoordinatePropertyName;
Title = title;
Color = color;
IndexesUsed = dataSourceIndexList;
}
private List<Point> _indexesUsed;
public List<Point> IndexesUsed
{
get
{
if (_indexesUsed != null) return _indexesUsed;
var ind = new List<Point>();
for (var i = 0; i < DataCountXCoordinate; i++)
for (var j = 0; j < DataCountYCoordinate; j++)
ind.Add(new Point(i, j));
return ind;
}
private set { _indexesUsed = value; }
}
private IList<double> DataSourceListXCoordinate { get; set; }
private IList<double> DataSourceListYCoordinate { get; set; }
private IList<double> DataSourceListZCoordinate { get; set; }
private int DataCountXCoordinate
{
get { return DataSourceListXCoordinate.Count; }
}
private int DataCountYCoordinate
{
get { return DataSourceListYCoordinate.Count; }
}
private string XCoordinatePropertyName { get; set; }
private string YCoordinatePropertyName { get; set; }
private string ZCoordinatePropertyName { get; set; }
public string XCoordinatePropertyDisplayName
{
get { return XCoordinatePropertyName + " (" + DataSourceListXCoordinate[0] + ")"; }
}
public string YCoordinatePropertyDisplayName
{
get { return YCoordinatePropertyName + " (" + DataSourceListYCoordinate[0] + ")"; }
}
public string ZCoordinatePropertyDisplayName
{
get { return ZCoordinatePropertyName + " (" + DataSourceListZCoordinate[0] + ")"; }
}
public string Title { get; private set; }
public Color Color { get; private set; }
public double? GetXValue(int i)
{
return DataSourceListXCoordinate[i];
}
public double? GetYValue(int i)
{
return DataSourceListYCoordinate[i];
}
public double? GetZValue(int i)
{
return DataSourceListZCoordinate[i];
}
public double GetMinX
{
get { return DataSourceListXCoordinate.Min(v => v); }
}
public double GetMaxX
{
get { return DataSourceListXCoordinate.Max(v => v); }
}
public double GetMinY
{
get { return DataSourceListYCoordinate.Min(v => v); }
}
public double GetMaxY
{
get { return DataSourceListYCoordinate.Max(v => v); }
}
public double GetMinZ
{
get { return DataSourceListZCoordinate.Min(v => v); }
}
public double GetMaxZ
{
get { return DataSourceListZCoordinate.Max(v => v); }
}
}
public interface IPlot3DSurface
{
List<Point> IndexesUsed { get; }
string XCoordinatePropertyDisplayName { get; }
string YCoordinatePropertyDisplayName { get; }
string ZCoordinatePropertyDisplayName { get; }
string Title { get; }
double? GetXValue(int i);
double? GetYValue(int i);
double? GetZValue(int i);
double GetMinX { get; }
double GetMaxX { get; }
double GetMinY { get; }
double GetMaxY { get; }
double GetMinZ { get; }
double GetMaxZ { get; }
Color Color { get; }
}
Surface
系列在使用ColorRange绘制单元格时,不使用笔刷绘制图例符号。
如果你想强制在图例符号处绘制系列颜色,你可以使用 OnSymbolDraw
事件:
tChart1.Legend.Symbol.OnSymbolDraw += Symbol_OnSymbolDraw;
并在其中手动绘制符号:
private void Symbol_OnSymbolDraw(object sender, SymbolDrawEventArgs e)
{
if (e.Series != null)
{
tChart1.Graphics3D.Brush.Color = e.Series.Color;
}
tChart1.Graphics3D.Rectangle(e.Rect);
}