Skip to content

Commit

Permalink
Support global config of gantt axisFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlong committed Mar 12, 2018
1 parent acf11bd commit 37076d3
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 61 deletions.
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</div>
<script src="./mermaid.js"></script>
<script>
mermaid.initialize({ theme: 'forest', logLevel: 3 });
mermaid.initialize({ theme: 'forest', logLevel: 3, gantt: { axisFormat: '%m/%d/%Y' } });
</script>
<script>
function testClick(nodeId) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mermaid",
"version": "8.0.0-alpha.3",
"version": "8.0.0-alpha.4",
"description": "Markdownish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
"main": "dist/mermaid.core.js",
"keywords": [
Expand Down
37 changes: 1 addition & 36 deletions src/diagrams/gantt/ganttRenderer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import moment from 'moment'
import * as d3 from 'd3'

import { parser } from './parser/gantt'
import ganttDb from './ganttDb'
import { logger } from '../../logger'

parser.yy = ganttDb

let daysInChart
const conf = {
titleTopMargin: 25,
barHeight: 20,
Expand Down Expand Up @@ -52,13 +49,6 @@ export const draw = function (text, id) {
elem.setAttribute('viewBox', '0 0 ' + w + ' ' + h)
const svg = d3.select('#' + id)

const startDate = d3.min(taskArray, function (d) {
return d.startTime
})
const endDate = d3.max(taskArray, function (d) {
return d.endTime
})

// Set timescale
const timeScale = d3.scaleTime()
.domain([d3.min(taskArray, function (d) {
Expand All @@ -71,8 +61,6 @@ export const draw = function (text, id) {

let categories = []

daysInChart = moment.duration(endDate - startDate).asDays()

for (let i = 0; i < taskArray.length; i++) {
categories.push(taskArray[i].type)
}
Expand Down Expand Up @@ -254,32 +242,9 @@ export const draw = function (text, id) {
}

function makeGrid (theSidePad, theTopPad, w, h) {
const formatMillisecond = d3.timeFormat('.%L')
const formatSecond = d3.timeFormat(':%S')
const formatMinute = d3.timeFormat('%I:%M')
const formatHour = d3.timeFormat('%I %p')
const formatDay = d3.timeFormat('%Y-%m-%d')
const formatWeek = d3.timeFormat('%Y-%m-%d')
const formatMonth = d3.timeFormat('%b-%Y')
const formatYear = d3.timeFormat('%Y')

const multiFormat = date => {
return (d3.timeSecond(date) < date ? formatMillisecond
: d3.timeMinute(date) < date ? formatSecond
: d3.timeHour(date) < date ? formatMinute
: d3.timeDay(date) < date ? formatHour
: d3.timeMonth(date) < date ? (d3.timeWeek(date) < date ? formatDay : formatWeek)
: d3.timeYear(date) < date ? formatMonth
: formatYear)(date)
}

// todo: if (typeof conf.axisFormatter !== 'undefined')

let xAxis = d3.axisBottom(timeScale)
.tickSize(-h + theTopPad + conf.gridLineStartPadding)
.tickFormat(multiFormat)

logger.debug(daysInChart) // just to pass lint
.tickFormat(d3.timeFormat(conf.axisFormat || '%Y-%m-%d'))

svg.append('g')
.attr('class', 'grid')
Expand Down
25 changes: 2 additions & 23 deletions src/mermaidAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,9 @@ const config = {
numberSectionStyles: 3,

/**
* **axisFormatter** - formatting of the axis, this might need adjustment to match your locale and preferences
* **axisFormat** - datetime format of the axis, this might need adjustment to match your locale and preferences
*/
axisFormatter: [
// Within a day
['%I:%M', function (d) {
return d.getHours()
}],
// Monday a week
['w. %U', function (d) {
return d.getDay() === 1
}],
// Day within a week (not monday)
['%a %d', function (d) {
return d.getDay() && d.getDate() !== 1
}],
// within a month
['%b %d', function (d) {
return d.getDate() !== 1
}],
// Month
['%m-%y', function (d) {
return d.getMonth()
}]
]
axisFormat: '%Y-%m-%d'
},
class: {},
git: {},
Expand Down

0 comments on commit 37076d3

Please sign in to comment.