Skip to content

Commit 1e9aa18

Browse files
committed
fix: gradient colors support 1 color
1 parent 23c9c5f commit 1e9aa18

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/charting/renderer/LineChartRenderer.ts

+13-21
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,10 @@ export class LineChartRenderer extends LineRadarRenderer {
446446
}
447447
}
448448

449-
getMultiColorsShader(colors: { color: string | Color; [k: string]: any }[], points, trans: Transformer, dataSet: LineDataSet, renderPaint: Paint) {
449+
getMultiColorsShader(colors: { color: string | Color; [k: string]: any }[], points, trans: Transformer, dataSet: LineDataSet) {
450450
const nbColors = colors.length;
451451
const xKey = dataSet.xProperty;
452-
if (nbColors > 1) {
452+
if (nbColors > 0) {
453453
trans.pointValuesToPixel(points);
454454
const shaderColors = [];
455455
const positions = [];
@@ -458,7 +458,7 @@ export class LineChartRenderer extends LineRadarRenderer {
458458
const lastIndex = firstIndex + range;
459459
const width = this.mViewPortHandler.getChartWidth();
460460
const chartRect = this.mViewPortHandler.getChartRect();
461-
let lastColor, lastColorPosX;
461+
let lastColor;
462462

463463
const gradientDelta = 0;
464464
const posDelta = gradientDelta / width;
@@ -489,17 +489,6 @@ export class LineChartRenderer extends LineRadarRenderer {
489489
}
490490
const posX = Math.floor(points[(colorIndex - firstIndex) * 2]);
491491
const pos = (posX - chartRect.left) / width;
492-
// if (posX - lastColorPosX < 3) {
493-
// // ignore too small
494-
// // lastColor = color.color;
495-
// shaderColors.pop();
496-
// shaderColors.pop();
497-
// positions.pop();
498-
// positions.pop();
499-
// lastColor = shaderColors[shaderColors.length -1];
500-
// lastColorPosX = positions[positions.length -1] * width + chartRect.left;
501-
// continue;
502-
// }
503492
if (lastColor) {
504493
if (shaderColors.length === 0) {
505494
shaderColors.push(lastColor);
@@ -511,13 +500,16 @@ export class LineChartRenderer extends LineRadarRenderer {
511500
shaderColors.push(color.color);
512501
positions.push(pos + posDelta);
513502
lastColor = color.color;
514-
lastColorPosX = posX;
515503
}
516-
if (shaderColors.length > 1) {
517-
return new LinearGradient(0, 0, width, 0, shaderColors, positions, TileMode.CLAMP);
518-
} else if (shaderColors.length === 1) {
519-
renderPaint.setColor(shaderColors[0]);
504+
if (shaderColors.length === 0) {
505+
shaderColors.push(colors[0].color);
506+
positions.push(0);
520507
}
508+
if (shaderColors.length === 1) {
509+
shaderColors.push(colors[0].color);
510+
positions.push(1);
511+
}
512+
return new LinearGradient(0, 0, width, 0, shaderColors, positions, TileMode.CLAMP);
521513
}
522514
return null;
523515
}
@@ -559,9 +551,9 @@ export class LineChartRenderer extends LineRadarRenderer {
559551
const nbColors = colors.length;
560552
const renderPaint = this.renderPaint;
561553
let paintColorsShader;
562-
if (nbColors > 1) {
554+
if (nbColors > 0) {
563555
// TODO: we transforms points in there. Could be dangerous if used after
564-
paintColorsShader = this.getMultiColorsShader(colors, points, trans, dataSet, renderPaint);
556+
paintColorsShader = this.getMultiColorsShader(colors, points, trans, dataSet);
565557
}
566558

567559
let oldShader;

0 commit comments

Comments
 (0)