-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating colorPalette and adding color to visual
- Loading branch information
wesyao
authored and
Wes
committed
Jul 30, 2016
1 parent
3c6e818
commit a521bc6
Showing
3 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
module powerbi.extensibility.visual { | ||
/** | ||
* Interface for Palette of colors. | ||
* | ||
* @interface | ||
* @property {IColorInfo} [key: string] - Map of key to IColorInfo. | ||
*/ | ||
interface IPalette { | ||
[key: string]: IColorInfo; | ||
} | ||
|
||
/** | ||
* Interface for IColorPalette. | ||
* | ||
* @interface | ||
*/ | ||
export interface IColorPalette { | ||
getColor(key: string): IColorInfo; | ||
reset(): IColorPalette; | ||
clear(): void; | ||
} | ||
|
||
/** | ||
* Singleton reference of ColorPalette. | ||
* | ||
* @instance | ||
*/ | ||
var colorManager: IColorPalette; | ||
|
||
/** | ||
* Factory method for creating a ColorPalette. | ||
* | ||
* @function | ||
* @param {IColorInfo[]} colors - Array of ColorInfo objects that contain | ||
* hex values for colors. | ||
*/ | ||
export function createColorPalette(colors: IColorInfo[]): IColorPalette { | ||
if(!colorManager) | ||
colorManager = new ColorPalette(colors); | ||
|
||
return colorManager; | ||
} | ||
|
||
class ColorPalette implements IColorPalette { | ||
private colorPalette: IPalette = {}; | ||
private colors: IColorInfo[]; | ||
private colorIndex: number = 0; | ||
|
||
constructor(colors: IColorInfo[]) { | ||
this.colors = colors; | ||
} | ||
|
||
/** | ||
* Gets color from colorPalette and returns an IColorInfo | ||
* | ||
* @function | ||
* @param {string} key - Key of assign color in colorPalette. | ||
*/ | ||
public getColor(key: string): IColorInfo { | ||
let color = this.colorPalette[key]; | ||
if(color) { | ||
return color; | ||
} | ||
|
||
let colors = this.colors; | ||
color = this.colorPalette[key] = colors[this.colorIndex++]; | ||
|
||
if(this.colorIndex >= colors.length) { | ||
this.colorIndex = 0; | ||
} | ||
|
||
return color; | ||
} | ||
|
||
/** | ||
* resets colorIndex to 0 | ||
* | ||
* @function | ||
*/ | ||
public reset(): IColorPalette { | ||
this.colorIndex = 0; | ||
return this; | ||
} | ||
|
||
/** | ||
* Clears colorPalette of cached keys and colors | ||
* | ||
* @function | ||
*/ | ||
public clear(): void { | ||
this.colorPalette = {}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit a521bc6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current template doesn't require you to create your own implementation of colorPalette
anymore. The colorPalette
is part of the IVisualHost
now. The only changes needed are:
interface BarChartDataPoint {
value: number;
category: string;
color: string;
};
...
let colorPalette: IColorPalette = host.colorPalette; // host: IVisualHost
for (let i = 0, len = Math.max(category.values.length, dataValue.values.length); i < len; i++) {
barChartDataPoints.push({
category: <string>category.values[i],
value: <number>dataValue.values[i],
color: colorPalette.getColor(<string>category.values[i]).value
});
}
And inside your update() method:
bars.attr({
width: xScale.rangeBand(),
height: d => height - yScale(d.value),
y: d => yScale(d.value),
x: d => xScale(d.category),
fill: d => d.color,
});
This has all been changed: 9cc6d06