如何使用核心图为图例设置不同颜色的文本
How to set different color of text for the legends using core plot
我正在使用 core plot 来绘制图表。我的问题是我想根据线条颜色设置图例文本颜色。我在一张图中有四个散点图,每个散点图都有不同的线条颜色。
在图例中它会正确显示线条颜色,但我想根据每个散点图标题的线条颜色更改文本颜色。我为图例编写了以下代码。
newGraph.legend = CPTLegend(graph: newGraph)
newGraph.legend?.numberOfRows = UInt(2.0)
let dataSourceLabelStyle = CPTMutableTextStyle()
dataSourceLabelStyle.color = CPTColor.white()
newGraph.legend?.textStyle = dataSourceLabelStyle
newGraph.legend?.fill = CPTFill(color: CPTColor.init(componentRed: 50/244, green: 43/244, blue: 87/244, alpha: 1.0))
newGraph.legend?.cornerRadius = 5.0
newGraph.legendDisplacement = CGPoint.init(x: -50, y: 210)
let fadeInAnimation = CABasicAnimation(keyPath: "opacity")
fadeInAnimation.duration = 1.0
fadeInAnimation.isRemovedOnCompletion = false
fadeInAnimation.fillMode = kCAFillModeForwards
fadeInAnimation.toValue = 1.0
dataSourceLinePlot.add(fadeInAnimation, forKey: "animateOpacity")
self.scatterGraph = newGraph
从上面的代码可以看出图例只能采用一种样式,即 newGraph.legend?.textStyle = dataSourceLabelStyle
所以图例中的所有文本都是白色的。
我也想根据线条颜色更改图例文本颜色。
谁能body帮我解决这个问题。
对于包括散点图在内的大多数绘图类型,将 attributedTitle
设置为具有所需颜色属性的 NSAttributedString
。饼图和条形图有数据源方法为每个数据索引提供属性图例标题。
不认为这是可能的,因为您为图表设置了图例,而图表包含您的所有图表。不过,这是我在 ObjC 中使用它的方式:
CPTScatterPlot *aPlot = [[CPTScatterPlot alloc] init];
aPlot.dataSource = self;
aPlot.identifier = stringPlotId;
[graph addPlot:aPlot toPlotSpace:plotSpace];
aPlot.title=stringPlotTitle;//will show in legends
//add more plots if needed then format labels text once for all plots
graph.legend= [CPTLegend legendWithGraph:graph];
CPTMutableTextStyle *mySmallerTextStyle = [[CPTMutableTextStyle alloc] init];
[mySmallerTextStyle setFontSize:8];
mySmallerTextStyle.color=[CPTColor whiteColor];
[graph.legend setTextStyle:(CPTTextStyle *)mySmallerTextStyle];
我正在使用 core plot 来绘制图表。我的问题是我想根据线条颜色设置图例文本颜色。我在一张图中有四个散点图,每个散点图都有不同的线条颜色。
在图例中它会正确显示线条颜色,但我想根据每个散点图标题的线条颜色更改文本颜色。我为图例编写了以下代码。
newGraph.legend = CPTLegend(graph: newGraph)
newGraph.legend?.numberOfRows = UInt(2.0)
let dataSourceLabelStyle = CPTMutableTextStyle()
dataSourceLabelStyle.color = CPTColor.white()
newGraph.legend?.textStyle = dataSourceLabelStyle
newGraph.legend?.fill = CPTFill(color: CPTColor.init(componentRed: 50/244, green: 43/244, blue: 87/244, alpha: 1.0))
newGraph.legend?.cornerRadius = 5.0
newGraph.legendDisplacement = CGPoint.init(x: -50, y: 210)
let fadeInAnimation = CABasicAnimation(keyPath: "opacity")
fadeInAnimation.duration = 1.0
fadeInAnimation.isRemovedOnCompletion = false
fadeInAnimation.fillMode = kCAFillModeForwards
fadeInAnimation.toValue = 1.0
dataSourceLinePlot.add(fadeInAnimation, forKey: "animateOpacity")
self.scatterGraph = newGraph
从上面的代码可以看出图例只能采用一种样式,即 newGraph.legend?.textStyle = dataSourceLabelStyle
所以图例中的所有文本都是白色的。
我也想根据线条颜色更改图例文本颜色。
谁能body帮我解决这个问题。
对于包括散点图在内的大多数绘图类型,将 attributedTitle
设置为具有所需颜色属性的 NSAttributedString
。饼图和条形图有数据源方法为每个数据索引提供属性图例标题。
不认为这是可能的,因为您为图表设置了图例,而图表包含您的所有图表。不过,这是我在 ObjC 中使用它的方式:
CPTScatterPlot *aPlot = [[CPTScatterPlot alloc] init];
aPlot.dataSource = self;
aPlot.identifier = stringPlotId;
[graph addPlot:aPlot toPlotSpace:plotSpace];
aPlot.title=stringPlotTitle;//will show in legends
//add more plots if needed then format labels text once for all plots
graph.legend= [CPTLegend legendWithGraph:graph];
CPTMutableTextStyle *mySmallerTextStyle = [[CPTMutableTextStyle alloc] init];
[mySmallerTextStyle setFontSize:8];
mySmallerTextStyle.color=[CPTColor whiteColor];
[graph.legend setTextStyle:(CPTTextStyle *)mySmallerTextStyle];