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

[Heatmap] Localize Tooltip Date formatting #162

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

and include in your project:
```js
import Chart from "frappe-charts/dist/frappe-charts.min.esm"
import Chart from "frappe-charts"
```

* ...or include within your HTML
Expand All @@ -63,11 +63,11 @@ const data = {
],
datasets: [
{
title: "Some Data", type: "bar",
name: "Some Data", type: "bar",
values: [25, 40, 30, 35, 8, 52, 17, -4]
},
{
title: "Another Set", type: "line",
name: "Another Set", type: "line",
values: [25, 50, -10, 15, 18, 32, 27, 14]
}
]
Expand Down
14 changes: 6 additions & 8 deletions dist/frappe-charts.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ class SvgTip {
}
}

/**
* Returns the value of a number upto 2 decimal places.
* @param {Number} d Any number
*/
function floatTwo(d) {
return parseFloat(d.toFixed(2));
}
Expand Down Expand Up @@ -2656,6 +2652,7 @@ class Heatmap extends BaseChart {
this.type = 'heatmap';

this.countLabel = options.countLabel || '';
this.dateFormat = options.dateFormat || { year: 'numeric', month: 'short', day: 'numeric' };

let validStarts = ['Sunday', 'Monday'];
let startSubDomain = validStarts.includes(options.startSubDomain)
Expand Down Expand Up @@ -2790,17 +2787,18 @@ class Heatmap extends BaseChart {
if(daySquares.includes(daySquare)) {

let count = daySquare.getAttribute('data-value');
let dateParts = daySquare.getAttribute('data-date').split('-');

let month = getMonthName(parseInt(dateParts[1])-1, true);
let date = new Date(daySquare.getAttribute('data-date'));

let gOff = this.container.getBoundingClientRect(), pOff = daySquare.getBoundingClientRect();

let width = parseInt(e.target.getAttribute('width'));
let x = pOff.left - gOff.left + width/2;
let y = pOff.top - gOff.top;
let value = count + ' ' + this.countLabel;
let name = ' on ' + month + ' ' + dateParts[0] + ', ' + dateParts[2];
let name = ' on ' + date.toLocaleDateString(
window.navigator.location || 'en-US',
this.dateFormat
);

this.tip.setValues(x, y, {name: name, value: value, valueFirst: 1}, []);
this.tip.showTip();
Expand Down
2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ let heatmapArgs = {
discreteDomains: 1,
countLabel: 'Level',
colors: HEATMAP_COLORS_BLUE,
dateFormat: { month: 'numeric', day: '2-digit' },
legendScale: [0, 1, 2, 4, 5]
};
let heatmapChart = new Chart("#chart-heatmap", heatmapArgs);
Expand Down
7 changes: 6 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ <h6 class="margin-vertical-rem">
end: endDate // Date objects
},
countLabel: 'Level',
dateFormat: { month: '2-digit', day: '2-digit' },
// Formats date in tooltip based on the end user's browser language and locale
// This value is used as the options parameter in Date.prototype.toLocaleDateString()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
// default: { year: 'numeric', month: 'short', day: 'numeric' }
discreteDomains: 0 // default: 1
colors: ['#ebedf0', '#c0ddf9', '#73b3f3', '#3886e1', '#17459e'],
// Set of five incremental colors,
Expand Down Expand Up @@ -315,7 +320,7 @@ <h6 class="margin-vertical-rem">Install</h6>
<p class="step-explain">And include it in your project</p>
<pre><code class="hljs javascript"> import { Chart } from "frappe-charts"</code></pre>
<p class="step-explain">... or include it directly in your HTML</p>
<pre><code class="hljs html"> &lt;script src="https://unpkg.com/frappe-charts@1.0.0"&gt;&lt;/script&gt;</code></pre>
<pre><code class="hljs html"> &lt;script src="https://unpkg.com/frappe-charts@1.1.0"&gt;&lt;/script&gt;</code></pre>

</div>
</div>
Expand Down
12 changes: 7 additions & 5 deletions src/js/charts/Heatmap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BaseChart from './BaseChart';
import { getComponent } from '../objects/ChartComponents';
import { makeText, heatSquare } from '../utils/draw';
import { DAY_NAMES_SHORT, addDays, areInSameMonth, getLastDateInMonth, setDayToSunday, getYyyyMmDd, getWeeksBetween, getMonthName, clone,
import { DAY_NAMES_SHORT, addDays, areInSameMonth, getLastDateInMonth, setDayToSunday, getYyyyMmDd, getWeeksBetween, clone,
NO_OF_MILLIS, NO_OF_YEAR_MONTHS, NO_OF_DAYS_IN_WEEK } from '../utils/date-utils';
import { calcDistribution, getMaxCheckpoint } from '../utils/intervals';
import { getExtraHeight, getExtraWidth, HEATMAP_DISTRIBUTION_SIZE, HEATMAP_SQUARE_SIZE,
Expand All @@ -17,6 +17,7 @@ export default class Heatmap extends BaseChart {
this.type = 'heatmap';

this.countLabel = options.countLabel || '';
this.dateFormat = options.dateFormat || { year: 'numeric', month: 'short', day: 'numeric' };

let validStarts = ['Sunday', 'Monday'];
let startSubDomain = validStarts.includes(options.startSubDomain)
Expand Down Expand Up @@ -151,17 +152,18 @@ export default class Heatmap extends BaseChart {
if(daySquares.includes(daySquare)) {

let count = daySquare.getAttribute('data-value');
let dateParts = daySquare.getAttribute('data-date').split('-');

let month = getMonthName(parseInt(dateParts[1])-1, true);
let date = new Date(daySquare.getAttribute('data-date'));

let gOff = this.container.getBoundingClientRect(), pOff = daySquare.getBoundingClientRect();

let width = parseInt(e.target.getAttribute('width'));
let x = pOff.left - gOff.left + width/2;
let y = pOff.top - gOff.top;
let value = count + ' ' + this.countLabel;
let name = ' on ' + month + ' ' + dateParts[0] + ', ' + dateParts[2];
let name = ' on ' + date.toLocaleDateString(
window.navigator.location || 'en-US',
this.dateFormat
);

this.tip.setValues(x, y, {name: name, value: value, valueFirst: 1}, []);
this.tip.showTip();
Expand Down