ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.55k stars 5.99k forks source link

the BubbleChartDataSet colors property is bound to chart view x-axis, not data-set entries (Charts 3.0.2) #2839

Open ernest-bruce opened 7 years ago

ernest-bruce commented 7 years ago

while working with bubble data sets and charts i noticed that in bubble data sets, even though the documentation for ChartBaseDataSet.colors says that colors are bound to data-set entries, when you assign colors to the data set and render it in a bubble chart view, Charts 3.0.2 bounds the data-set colors to the x-axis range, not the data-set entries. line 95 in BubbleChartRenderer.swift confirms this: for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)

that’s different from a similar line (299) in BarChartRenderer.swift: for j in stride(from: 0, to: buffer.rects.count, by: 1)

i had a hard time understanding why my color assignments weren’t being followed in bubble charts. the documentation for the BubbleChartDataSet and BubbleChartRenderer classes don’t mention this diversion from the guidance provided in ChartBaseDataSet.swift for the colors property.

however, because i have to “expand” my colors array to include slots for all the items in the x-axis to have the colors correctly assigned to the data entries on the chart, i wonder whether the issue is in the documentation or the implementation.

attached are my supporting materials.

bubble_color_mismatch bubble_color_ok

iOSChartsDemo.zip

liuxuan30 commented 7 years ago

I'm trying to understand your question, but I don't understand your image. Are you asking you want to use entry index in the data set, instead of the x?

current bubble chart uses let color = dataSet.color(atIndex: Int(entry.x)) to determine the color. However, using the entry index seems equivalent to using entry.x here, because basically Int(entry.x) = j in bubble chart (unless you have specified user case where x is decimal rather than int).

for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
{
     guard let entry = dataSet.entryForIndex(j) as? BubbleChartDataEntry else { continue }

what I debugged and print out j and Int(entry.x) is the same here.

if your x is decimal here and x=2.1, x=2.2 are two different x grid lines for you, Int(2.1) and Int(2.2) will lead to the same color while you want them two different color?