Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow axis font size change and separate styles for x and y axis #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Axis.prototype = {
this.maxLabel = T.dimensions(
maxLabel,
{size:options.fontSize, angle: Flotr.toRad(this.options.labelsAngle)},
'font-size:smaller;',
'font-size:' + this.options.fontSize + ';',
'flotr-grid-label'
);

Expand Down
2 changes: 2 additions & 0 deletions js/DefaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Flotr.defaultOptions = {
showLabels: true, // => setting to true will show the axis ticks labels, hide otherwise
showMinorLabels: false,// => true to show the axis minor ticks labels, false to hide
labelsAngle: 0, // => labels' angle, in degrees
fontSize: 'smaller', // => font size of the xaxis labels
title: null, // => axis title
titleAngle: 0, // => axis title's angle, in degrees
noTicks: 5, // => number of ticks for automagically generated ticks
Expand Down Expand Up @@ -47,6 +48,7 @@ Flotr.defaultOptions = {
showLabels: true, // => setting to true will show the axis ticks labels, hide otherwise
showMinorLabels: false,// => true to show the axis minor ticks labels, false to hide
labelsAngle: 0, // => labels' angle, in degrees
fontSize: 'smaller', // => font size of the xaxis labels - should be a string
title: null, // => axis title
titleAngle: 90, // => axis title's angle, in degrees
noTicks: 5, // => number of ticks for automagically generated ticks
Expand Down
39 changes: 20 additions & 19 deletions js/plugins/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@ Flotr.addPlugin('labels', {
draw: function(){
// Construct fixed width label boxes, which can be styled easily.
var
axis, tick, left, top, xBoxWidth,
axis, tick, left, top,
radius, sides, coeff, angle,
div, i, html = '',
noLabels = 0,
options = this.options,
ctx = this.ctx,
a = this.axes,
style = { size: options.fontSize };

for (i = 0; i < a.x.ticks.length; ++i){
if (a.x.ticks[i].label) { ++noLabels; }
}
xBoxWidth = this.plotWidth / noLabels;

if (options.grid.circular) {
ctx.save();
ctx.translate(this.plotOffset.left + this.plotWidth / 2,
Expand Down Expand Up @@ -67,14 +61,6 @@ Flotr.addPlugin('labels', {

ctx.stroke();
ctx.restore();
div = D.create('div');
D.setStyles(div, {
fontSize: 'smaller',
color: options.grid.color
});
div.className = 'flotr-labels';
D.insert(this.el, div);
D.insert(div, html);
}

function drawLabelCircular (graph, axis, minorTicks) {
Expand Down Expand Up @@ -175,7 +161,8 @@ Flotr.addPlugin('labels', {
isFirst = axis.n === 1,
name = '',
left, style, top,
offset = graph.plotOffset;
offset = graph.plotOffset,
i, xBoxWidth, noLabels = 0;

if (!isX && !isFirst) {
ctx.save();
Expand All @@ -184,16 +171,21 @@ Flotr.addPlugin('labels', {
}

if (axis.options.showLabels && (isFirst ? true : axis.used)) {
for (i = 0; i < axis.ticks.length; ++i){
if (axis.ticks[i].label) { ++noLabels; }
}
xBoxWidth = graph.plotWidth / noLabels;

for (i = 0; i < axis.ticks.length; ++i) {
tick = axis.ticks[i];
if (!tick.label || !tick.label.length ||
((isX ? offset.left : offset.top) + axis.d2p(tick.v) < 0) ||
((isX ? offset.left : offset.top) + axis.d2p(tick.v) > (isX ? graph.canvasWidth : graph.canvasHeight))) {
((isX ? offset.left : offset.top) + axis.d2p(tick.v) < (isX ? (offset.left + options.grid.labelMargin) : 0)) ||
((isX ? offset.left : offset.top) + axis.d2p(tick.v) > (isX ? graph.canvasWidth - offset.right - ctx.measureText(tick.label).width : graph.canvasHeight))) {
continue;
}
top = offset.top +
(isX ?
((isFirst ? 1 : -1 ) * (graph.plotHeight + options.grid.labelMargin)) :
(options.grid.labelMargin + (isFirst ? graph.plotHeight : 0)) :
axis.d2p(tick.v) - axis.maxLabel.height / 2);
left = isX ? (offset.left + axis.d2p(tick.v) - xBoxWidth / 2) : 0;

Expand All @@ -219,6 +211,15 @@ Flotr.addPlugin('labels', {
ctx.lineTo(offset.left + graph.plotWidth, offset.top + axis.d2p(tick.v));
}
}
var div = D.create('div');
D.setStyles(div, {
fontSize: axis.options.fontSize,
color: options.grid.color
});
div.className = 'flotr-labels';
D.insert(graph.el, div);
D.insert(div, html);
html = '';
}
}
}
Expand Down