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

Export as Module, simplifyed code HorizontalBars #3

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var defaultOptions = {
```

## Sample usage in Chartist.js
## if horizontalBars is true the goalLine is draw verticaly

```javascript
var chart = new Chartist.Line('.ct-chart', {
Expand Down
83 changes: 59 additions & 24 deletions chartist-plugin-targetline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,72 @@
* Chartist.js plugin to display a target line on a chart.
* With code from @gionkunz in https://github.com/gionkunz/chartist-js/issues/235
* and @OscarGodson in https://github.com/gionkunz/chartist-js/issues/491.
* and @EmersonBottero in https://github.com/gionkunz/chartist-js/issues/235
* Based on https://github.com/gionkunz/chartist-plugin-pointlabels
*/
/* global Chartist */
(function(window, document, Chartist) {
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(["chartist"], function (Chartist) {
return (root.returnExportsGlobal = factory(Chartist));
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(require("chartist"));
} else {
root['Chartist.plugins.targetline'] = factory(Chartist);
}
}(this, function (Chartist) {
/**
* Chartist.js plugin to display a data label on top of the points in a line chart.
*
*/
/* global Chartist */
(function(window, document, Chartist) {
'use strict';

var defaultOptions = {
class: 'ct-target-line',
value: null
};

Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctTargetLine = function(options) {
var defaultOptions = {
class: 'ct-target-line',
value: null
};

options = Chartist.extend({}, defaultOptions, options);
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctTargetLine = function(options) {

return function ctTargetLine(chart) {
function projectY(chartRect, bounds, value) {
return chartRect.y1 - (chartRect.height() / bounds.max * value)
}
options = Chartist.extend({}, defaultOptions, options);

return function ctTargetLine(chart) {
chart.on('created', function (context) {
var targetLineY = projectY(context.chartRect, context.bounds, options.value);

context.svg.elem('line', {
x1: context.chartRect.x1,
x2: context.chartRect.x2,
y1: targetLineY,
y2: targetLineY
}, options.class);

if (context.options.horizontalBars ){ //vertical target bar
var targetLineX = context.chartRect.x1 + context.axisX.projectValue(options.value);

context.svg.elem('line', {
x1: targetLineX,
x2: targetLineX,
y1: context.chartRect.y1,
y2: context.chartRect.y2
}, options.class);

} else {//horizontal target bar
var targetLineY =context.chartRect.y1 - context.axisY.projectValue(options.value);

context.svg.elem('line', {
x1: context.chartRect.x1,
x2: context.chartRect.x2,
y1: targetLineY,
y2: targetLineY
}, options.class);

}
});
};
};
};
}(window, document, Chartist));
}(window, document, Chartist));

return Chartist.plugins.targetline;

}));
Binary file modified chartist-plugin-targetline.min.js
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
],
"author": "Harry Twyford",
"license": "ISC",
"devDependencies": {
"chartist": "~0.7.3"
},
"bugs": {
"url": "https://github.com/htwyford/chartist-plugin-targetline/issues"
},
Expand Down