|
function pcCreateSVG(el, dim, margin) { |
|
|
|
// create SVG element |
|
var svg = d3.select(el) |
|
.append("svg") |
|
.attr('class', 'pc-svg') |
|
.attr('width', dim.w) |
|
.attr('height', dim.h) |
|
.append("g") |
|
.attr('class', 'pc-main-g') |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") |
|
.attr('width', dim.cw) |
|
.attr('height', dim.ch); |
|
|
|
// add sub group containers |
|
svg.append('g').attr('class', 'pc-svg-bg-g' ); |
|
svg.append('g').attr('class', 'pc-chart-bg-g' ); |
|
svg.append('g').attr('class', 'pc-grid-g' ); |
|
svg.append('g').attr('class', 'pc-axes-g' ); |
|
svg.append('g').attr('class', 'pc-bars-g'); |
|
svg.append('g').attr('class', 'pc-legend-g'); |
|
svg.append('g').attr('class', 'pc-labels-g' ); |
|
|
|
return svg; |
|
} |
|
|
|
// set chart width/height based on container, and aspect ratio |
|
function pcChartDimensions(el, ar, margin) { |
|
|
|
// calc chart container width / height |
|
var w = document.querySelector(el).offsetWidth; |
|
var h = w * ar; |
|
|
|
// set height of container to match |
|
document.querySelector(el).style.height = h + "px"; |
|
|
|
// chart width / height |
|
var cw = w - margin.left - margin.right; |
|
var ch = h - margin.top - margin.bottom; |
|
|
|
return { |
|
w: w, |
|
h: h, |
|
cw: cw, |
|
ch: ch |
|
}; |
|
} |
|
|
|
function pcSetupVisibleElements(svg, dim, z, margin, ve, fields) { |
|
svg.select('.pc-axes-g') |
|
.append("g") |
|
.attr("class", "axis x-axis") |
|
.attr("transform", "translate(0," + dim.ch + ")"); |
|
|
|
svg.select('.pc-axes-g') |
|
.append("g") |
|
.attr("class", "axis y-axis"); |
|
|
|
if( ve.legend) { |
|
svg.select('.pc-legend-g') |
|
.attr("font-family", "sans-serif") |
|
.attr("font-size", 10) |
|
.attr("text-anchor", "end"); |
|
} |
|
|
|
// text label for the x axis |
|
if(ve.xLbl) { |
|
svg.select('.pc-labels-g') |
|
.append('text') |
|
.attr("class", "x-axis-label") |
|
.attr("dy", "1em") |
|
.style("text-anchor", "middle"); |
|
} |
|
|
|
// text label for the y axis |
|
if(ve.yLbl) { |
|
svg.select('.pc-labels-g') |
|
.append('text') |
|
.attr("class", "y-axis-label") |
|
.attr("transform", "rotate(-90)") |
|
.attr("dy", "1em") |
|
.style("text-anchor", "middle"); |
|
} |
|
|
|
// text label for the chart title |
|
if(ve.chartLbl) { |
|
svg.select('.pc-labels-g') |
|
.append('text') |
|
.attr("class", "chart-label") |
|
.attr("dy", "1em") |
|
.style("font-size", "16px") |
|
.style("text-anchor", "middle"); |
|
} |
|
|
|
// create svg bg rect |
|
svg.select('.pc-svg-bg-g') |
|
.append('rect') |
|
.attr('class', 'svg-area') |
|
.attrs({ |
|
x: 0 - margin.left, |
|
y: 0 - margin.top, |
|
width: dim.w, |
|
height: dim.h |
|
}); |
|
|
|
// create chart bg rect |
|
svg.select('.pc-chart-bg-g') |
|
.append('rect') |
|
.attr('class', 'chart-area') |
|
.attrs({ |
|
x: 0, |
|
y: 0, |
|
width: dim.cw, |
|
height: dim.ch |
|
}); |
|
} |
|
|
|
function pcUpdateVisibleElements(svg, visibleEl, keys, dim, chartOffset, z, lblTxt, margin) { |
|
|
|
// text label for the chart title |
|
if(visibleEl.chartLbl) { |
|
svg.select('.chart-label') |
|
.attrs({ |
|
x: (dim.cw / 2), |
|
y: 0 - (margin.top/2) - 10 |
|
}) |
|
.text(lblTxt.title); |
|
} |
|
|
|
// text label for the x axis |
|
if(visibleEl.xLbl) { |
|
if(lblTxt.x === '') lblTxt.x = fields[0]; |
|
svg.select('.x-axis-label') |
|
.attrs({ |
|
transform: "translate(" + (dim.cw / 2) + " ," + (dim.ch + 12) + ")" |
|
}) |
|
.text(lblTxt.x); |
|
} |
|
|
|
// text label for the y axis |
|
if(visibleEl.yLbl) { |
|
if(lblTxt.y === '') lblTxt.y = fields[1]; |
|
svg.select('.y-axis-label') |
|
.attrs({ |
|
x: 0 - (dim.ch / 2), |
|
y: 10 - margin.left |
|
}) |
|
.text(lblTxt.y); |
|
} |
|
|
|
// update legend if enabled |
|
if(visibleEl.legend) { |
|
legend = svg.select('.pc-legend-g') |
|
.selectAll("g") |
|
.data(keys) |
|
.enter().append("g") |
|
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); |
|
legend.append("rect").attr("x", dim.cw - chartOffset.legend).attr("y", 6).attr("width", 16).attr("height", 16).attr("fill", z); |
|
legend.append("text").attr("x", dim.cw - chartOffset.legend - 5).attr("y", 14).attr("dy", "0.32em").text(function(d) { return d; }); |
|
} |
|
} |