Skip to content

Commit

Permalink
feat(core): addition of titles within svg chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Natasha DeCoste committed Jun 27, 2019
1 parent d0df5bc commit 56380bb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/core/src/base-axis-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class BaseAxisChart extends BaseChart {
*************************************/
// TODO - Refactor
getChartSize(container = this.container) {
let ratio, marginForLegendTop;
let ratio, marginForLegendTop, marginForChartTitle;
if (container.node().clientWidth > Configuration.charts.widthBreak) {
ratio = Configuration.charts.magicRatio;
marginForLegendTop = 0;
Expand All @@ -163,8 +163,11 @@ export class BaseAxisChart extends BaseChart {

// Store computed actual size, to be considered for change if chart does not support axis
const marginsToExclude = Configuration.charts.margin.left + Configuration.charts.margin.right;

marginForChartTitle = this.options.chartTitle ? Configuration.charts.marginForChartTitle : 0;

const computedChartSize = {
height: container.node().clientHeight - marginForLegendTop,
height: container.node().clientHeight - marginForLegendTop - marginForChartTitle,
width: (container.node().clientWidth - marginsToExclude) * ratio
};

Expand Down
34 changes: 32 additions & 2 deletions packages/core/src/base-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class BaseChart {
// Perform the draw or update chart
if (initialDraw) {
this.initialDraw();
this.drawTitle(this.options.chartTitle);
} else {
if (removedItems.length > 0 || newItems.length > 0) {
this.addOrUpdateLegend();
Expand Down Expand Up @@ -270,7 +271,7 @@ export class BaseChart {

// TODO - Refactor
getChartSize(container = this.container) {
let ratio, marginForLegendTop;
let ratio, marginForLegendTop, marginForChartTitle;
if (container.node().clientWidth > Configuration.charts.widthBreak) {
ratio = Configuration.charts.magicRatio;
marginForLegendTop = 0;
Expand All @@ -281,8 +282,11 @@ export class BaseChart {

// Store computed actual size, to be considered for change if chart does not support axis
const marginsToExclude = 0;

marginForChartTitle = this.options.chartTitle ? Configuration.charts.marginForChartTitle : 0;

const computedChartSize = {
height: container.node().clientHeight - marginForLegendTop,
height: container.node().clientHeight - marginForLegendTop - marginForChartTitle,
width: (container.node().clientWidth - marginsToExclude) * ratio
};

Expand Down Expand Up @@ -363,6 +367,32 @@ export class BaseChart {
resizeObserver.observe(this.holder);
}

/**
* Draws title within the Chart's svg element.
* @param title
*/
drawTitle(title: string) {
if (title) {
this.svg
.append("text")
.classed("chart-title", true)
.text(title);
}
const titleMargin = Configuration.charts.marginForChartTitle;


// retrieve the current transformations (specific to each chart type)
const transform = this.innerWrap.attr("transform");
const translateArr = transform.substring(transform.indexOf("(") + 1, transform.indexOf(")")).split(",");


// add padding for title, keep other translations
this.innerWrap
.attr("transform", `translate(${translateArr[0]}, ${+translateArr[1] + titleMargin})`);


}

setClickableLegend() {
const self = this;
const c = select(this.holder);
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export interface BaseChartOptions {
* boolean to enable/disable legend interactivity
*/
legendClickable?: boolean;
/**
* optional title for chart
*/
chartTitle?: string;
/**
* boolean to prevent the container from resizing
*/
Expand Down Expand Up @@ -501,6 +505,7 @@ export const charts = {
minWidth: 150,
widthBreak: 600,
marginForLegendTop: 40,
marginForChartTitle: 24,
magicRatio: 0.7,
magicMoreForY2Axis: 70,
axisCharts: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/pie-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class PieChart extends BaseChart {
.attr("height", `${diameter}px`);

this.innerWrap
.style("transform", `translate(${radius}px,${radius}px)`)
.attr("transform", `translate(${radius},${radius})`)
.attr("width", `${diameter}px`)
.attr("height", `${diameter}px`)
.attr("preserveAspectRatio", "xMinYMin");
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ div.chart-holder {
position: relative;
@include fonts();


// Chart elements
svg.chart-svg {
overflow: visible !important;

/*
Chart titles
*/
.chart-title {
font-size: 14px;
line-height: 16px;
font-weight: 600;
padding-bottom: 8px;
dominant-baseline: hanging;
}

/*
Axes
*/
Expand Down Expand Up @@ -148,7 +160,9 @@ div.chart-holder {
}

text {
font-family: "IBM Plex Sans condensed";
font-size: 12px;
line-height: 16px;
max-width: 100px;
text-overflow: ellipsis;
white-space: nowrap;
Expand Down

0 comments on commit 56380bb

Please sign in to comment.