From 6ab7eb55cb299ee11fe57afa352007f88b0b17fa Mon Sep 17 00:00:00 2001 From: ashishj Date: Thu, 15 Dec 2022 20:14:49 +0100 Subject: [PATCH] Updated styling and added documentation --- .vite/build.ts | 2 +- packages/mermaid-timeline/src/mermaidUtils.ts | 12 +- packages/mermaid-timeline/src/styles.js | 5 +- .../mermaid-timeline/src/timelineRenderer.ts | 17 +- .../mermaid/src/docs/.vitepress/config.ts | 1 + .../src/docs/.vitepress/theme/mermaid.ts | 3 +- packages/mermaid/src/docs/syntax/timeline.md | 300 ++++++++++++++++++ packages/mermaid/src/docs/vite.config.ts | 4 + 8 files changed, 325 insertions(+), 19 deletions(-) create mode 100644 packages/mermaid/src/docs/syntax/timeline.md diff --git a/.vite/build.ts b/.vite/build.ts index 1e9f12a41..45c96cb2f 100644 --- a/.vite/build.ts +++ b/.vite/build.ts @@ -44,7 +44,7 @@ const packageOptions = { 'mermaid-timeline': { name: 'mermaid-timeline', packageName: 'mermaid-timeline', - file: 'diagram-definition.ts', + file: 'detector.ts', }, // 'mermaid-timeline-detector': { // name: 'mermaid-timeline-detector', diff --git a/packages/mermaid-timeline/src/mermaidUtils.ts b/packages/mermaid-timeline/src/mermaidUtils.ts index 7dc3732a8..1061f87cf 100644 --- a/packages/mermaid-timeline/src/mermaidUtils.ts +++ b/packages/mermaid-timeline/src/mermaidUtils.ts @@ -39,12 +39,12 @@ export let setupGraphViewbox: ( /** * Function called by mermaid that injects utility functions that help the diagram to be a good citizen. - * @param _log - * @param _setLogLevel - * @param _getConfig - * @param _sanitizeText - * @param _setupGraphViewbox - * @param _commonDb + * @param _log - The log function to use + * @param _setLogLevel - The function to set the log level + * @param _getConfig - The function to get the configuration + * @param _sanitizeText - The function to sanitize text + * @param _setupGraphViewbox - The function to setup the graph view-box + * @param _commonDb - The common database */ export const injectUtils = ( _log: Record, diff --git a/packages/mermaid-timeline/src/styles.js b/packages/mermaid-timeline/src/styles.js index 51d69f58d..fd9526e98 100644 --- a/packages/mermaid-timeline/src/styles.js +++ b/packages/mermaid-timeline/src/styles.js @@ -38,6 +38,10 @@ const genSections = (options) => { stroke-width: 3; } + .lineWrapper line{ + stroke: ${options['cScaleLabel' + i]} ; + } + .disabled, .disabled circle, .disabled text { fill: lightgray; } @@ -72,7 +76,6 @@ const getStyles = (options) => } .eventWrapper { filter: brightness(120%); - } `; export default getStyles; diff --git a/packages/mermaid-timeline/src/timelineRenderer.ts b/packages/mermaid-timeline/src/timelineRenderer.ts index 6fd68cf60..063d070a7 100644 --- a/packages/mermaid-timeline/src/timelineRenderer.ts +++ b/packages/mermaid-timeline/src/timelineRenderer.ts @@ -1,7 +1,6 @@ // @ts-nocheck TODO: fix file import { select } from 'd3'; import svgDraw from './svgDraw'; -import { configureSvgSize } from '../../setupGraphViewbox'; import addSVGAccessibilityFields from '../../accessibility'; import { log, getConfig, setupGraphViewbox } from './mermaidUtils'; @@ -91,8 +90,7 @@ export const draw = function (text, id, version, diagObj) { log.info('tasks.length', tasks.length); //calculate max task height // for loop till tasks.length - for (let i = 0; i < tasks.length; i++) { - const task = tasks[i]; + for (const [i, task] of tasks.entries()) { const taskNode = { number: i, @@ -190,9 +188,11 @@ export const draw = function (text, id, version, diagObj) { .attr('y', 20); } //5. Draw the diagram - depthY = hasSections?maxSectionHeight + maxTaskHeight + 150: maxTaskHeight + 100; + depthY = hasSections ? maxSectionHeight + maxTaskHeight + 150 : maxTaskHeight + 100; + + const lineWrapper = svg.append('g').attr('class', 'lineWrapper'); // Draw activity line - svg + lineWrapper .append('line') .attr('x1', LEFT_MARGIN) .attr('y1', depthY) // One section head + one task + margins @@ -211,9 +211,7 @@ export const draw = function (text, id, version, diagObj) { export const drawTasks = function (diagram, tasks, sectionColor, masterX, masterY, maxTaskHeight,conf,maxEventCount,maxEventLineLength,maxSectionHeight, isWithoutSections) { // Draw the tasks - for (let i = 0; i < tasks.length; i++) { - - const task = tasks[i]; + for (const task of tasks) { // create node from task const taskNode = { descr: task.task, @@ -283,8 +281,7 @@ export const drawEvents = function (diagram, events, sectionColor, masterX, mast const eventBeginY = masterY; masterY = masterY + 100 // Draw the events - for (let i = 0; i < events.length; i++) { - const event = events[i]; + for (const event of events) { // create node from event const eventNode = { descr: event, diff --git a/packages/mermaid/src/docs/.vitepress/config.ts b/packages/mermaid/src/docs/.vitepress/config.ts index 216541d52..74ed5e42c 100644 --- a/packages/mermaid/src/docs/.vitepress/config.ts +++ b/packages/mermaid/src/docs/.vitepress/config.ts @@ -107,6 +107,7 @@ function sidebarSyntax() { { text: 'Gitgraph (Git) Diagram 🔥', link: '/syntax/gitgraph' }, { text: 'C4C Diagram (Context) Diagram 🦺⚠️', link: '/syntax/c4c' }, { text: 'Mindmaps 🔥', link: '/syntax/mindmap' }, + { text: 'Timeline 🔥', link: '/syntax/timeline' }, { text: 'Other Examples', link: '/syntax/examples' }, ], }, diff --git a/packages/mermaid/src/docs/.vitepress/theme/mermaid.ts b/packages/mermaid/src/docs/.vitepress/theme/mermaid.ts index b287346f9..556eac4f5 100644 --- a/packages/mermaid/src/docs/.vitepress/theme/mermaid.ts +++ b/packages/mermaid/src/docs/.vitepress/theme/mermaid.ts @@ -1,8 +1,9 @@ import mermaid, { type MermaidConfig } from 'mermaid'; import mindmap from '@mermaid-js/mermaid-mindmap'; +import timeline from '@mermaid-js/mermaid-timeline'; try { - await mermaid.registerExternalDiagrams([mindmap]); + await mermaid.registerExternalDiagrams([mindmap,timeline]); } catch (e) { console.error(e); } diff --git a/packages/mermaid/src/docs/syntax/timeline.md b/packages/mermaid/src/docs/syntax/timeline.md new file mode 100644 index 000000000..571072ec9 --- /dev/null +++ b/packages/mermaid/src/docs/syntax/timeline.md @@ -0,0 +1,300 @@ +# Timeline Diagram + +> Timeline: This is an experimental diagram for now. The syntax and properties can change in future releases. The syntax is stable except for the icon integration which is the experimental part. + +"A timeline is a type of diagram used to illustrate a chronology of events, dates, or periods of time. It is usually presented graphically to indicate the passing of time, and it is usually organized chronologically. A basic timeline presents a list of events in chronological order, usually using dates as markers. A timeline can also be used to show the relationship between events, such as the relationship between the events of a person's life. A timeline can also be used to show the relationship between events, such as the relationship between the events of a person's life." Wikipedia + +### An example of a timeline. + +```mermaid +timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + + +## Syntax + +The syntax for creating Timeline diagram is simple. You always start with the `timeline` keyword to let mermaid know that you want to create a timeline diagram. + +After that there is a possibility to add a title to the timeline. This is done by adding a line with the keyword `title` followed by the title text. + +Then you add the timeline data, where you always start with a time period, followed by a colon and then the text for the event. Optionally you can add a second colon and then the text for the event. So, you can have one or more events per time period. + + +```json +{time period} : {event} +``` +or +```json +{time period} : {event} : {event} +``` +or +```json +{time period} : {event} + : {event} + : {event} +``` + +NOTE: Both time period and event are simple text, and not limited to numbers. + + +Let us look at the syntax for the example above. + +```mermaid-example +timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + +In this way we can use a text outline to generate a timeline diagram. +The sequence of time period and events is important, as it will be used to draw the timeline. The first time period will be placed at the left side of the timeline, and the last time period will be placed at the right side of the timeline. + +Similarly, the first event will be placed at the top for that specific time period, and the last event will be placed at the bottom. + +## Grouping of time periods in sections/ages +You can group time periods in sections/ages. This is done by adding a line with the keyword `section` followed by the section name. + +All subsequent time periods will be placed in this section until a new section is defined. + +If no section is defined, all time periods will be placed in the default section. + +Let us look at an example, where we have grouped the time periods in sections. + +```mermaid-example +timeline + title Timeline of Industrial Revolution + section 17th-20th century + Industry 1.0 : Machinery, Water power, Steam
power + Industry 2.0 : Electricity, Internal combustion engine, Mass production + Industry 3.0 : Electronics, Computers, Automation + section 21st century + Industry 4.0 : Internet, Robotics, Internet of Things + Industry 5.0 : Artificial intelligence, Big data,3D printing + Industry 6.0 : Quantum computing, Nanotechnology : Human-Cyber Interface +``` +As you can see, the time periods are placed in the sections, and the sections are placed in the order they are defined. + +All time periods and events under a given section follow a similar color scheme. This is done to make it easier to see the relationship between time periods and events. + + +## Wrapping of text for long time-periods or events +By default, the text for time-periods and events will be wrapped if it is too long. This is done to avoid that the text is drawn outside the diagram. + +You can also use `
` to force a line break. + +Let us look at another example, where we have a long time period, and a long event. + +```mermaid-example +timeline + title England's History Timeline + section Stone Age + 7600 BC : Britain's oldest known house was built in Orkney, Scotland + 6000 BC : Sea levels rise and Britain becomes an island.
The people who live here are hunter-gatherers. + section Broze Age + 2300 BC : People arrive from Europe and settle in Britain.
They bring farming and metalworking. + : New styles of pottery and ways of burying the dead appear. + 2200 BC : The last major building works are completed at Stonehenge.
People now bury their dead in stone circles. + : The first metal objects are made in Britain.Some other nice things happen. it is a good time to be alive. + +``` + + +## Styling of time periods and events +As explained earlier, each section has a color scheme, and each time period and event under a section follow the similar color scheme. + +However, if there is no section defined, then we have two possibilities: + +1. Style time periods individually, i.e. each time period(and its coressponding events) will have its own color scheme. This is the DEFAULT behavior. +```mermaid-example + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` +Note that this is no, section defined, and each time period and its corresponding events will have its own color scheme. + +2. Disable the multiColor option using the `disableMultiColor` option. This will make all time periods and events follow the same color scheme. + +You will need to add this option either via mermaid.intialize function or directives. + +```javascript +mermaid.initialize({ + theme: 'base', + startOnLoad: true, + logLevel: 0, + timeline: { + disableMulticolor: false, + }, + ... + ... +``` + +let us look at same example, where we have disabled the multiColor option. +```mermaid-example + %%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` +### Customizing Color scheme + +You can customize the color scheme using the `cScale0` to `cScale11` theme variables. Mermaid allows you to set unique colors for up-to 12, where `cScale0` variable will drive the value of the first section or time-period, `cScale1` will drive the value of the second section and so on. +In case you have more than 12 sections, the color scheme will start to repeat. + +NOTE: Default values for these theme variables are picked from the selected theme. If you want to override the default values, you can use the `initialize` call to add your custom theme variable values. + +Example: + +Now let's override the default values for the `cScale0` to `cScale2` variables: + +```mermaid-example + %%{init: { 'logLevel': 'debug', 'theme': 'default' , 'themeVariables': { + 'cScale0': '#ff0000', + 'cScale1': '#00ff00', + 'cScale2': '#0000ff', + } } }%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest + +``` + +See how the colors are changed to the values specified in the theme variables. + + +## Themes +Mermaid supports a bunch of pre-defined themes which you can use to find the right one for you. PS: you can actually override an existing theme's variable to get your own custom theme going. Learn more about theming your diagram [here](../config/theming.md). + +The following are the different pre-defined theme options: + +- `base` +- `forest` +- `dark` +- `default` +- `neutral` + +**NOTE**: To change theme you can either use the `initialize` call or _directives_. Learn more about [directives](../config/directives.md) +Let's put them to use, and see how our sample diagram looks in different themes: + +### Base Theme + +```mermaid-example +%%{init: { 'logLevel': 'debug', 'theme': 'base' } }%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + +### Forest Theme + +```mermaid-example +%%{init: { 'logLevel': 'debug', 'theme': 'forest' } }%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + +### Dark Theme + +```mermaid-example +%%{init: { 'logLevel': 'debug', 'theme': 'dark' } }%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + +### Default Theme + +```mermaid-example +%%{init: { 'logLevel': 'debug', 'theme': 'default' } }%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + +### Neutral Theme + +```mermaid-example +%%{init: { 'logLevel': 'debug', 'theme': 'neutral' } }%% + timeline + title History of Social Media Platform + 2002 : LinkedIn + 2004 : Facebook : Google + 2005 : Youtube + 2006 : Twitter + 2007 : Tumblr + 2008 : Instagram + 2010 : Pinterest +``` + + + + + +## Integrating with your library/website. + +Timeline uses the experimental lazy loading & async rendering features which could change in the future. + +```html + +``` + +You can also refer the implementation in the live editor [here](https://github.com/mermaid-js/mermaid-live-editor/blob/fcf53c98c25604c90a218104268c339be53035a6/src/lib/util/mermaid.ts) to see how the async loading is done. diff --git a/packages/mermaid/src/docs/vite.config.ts b/packages/mermaid/src/docs/vite.config.ts index 15652c21c..a1d92c736 100644 --- a/packages/mermaid/src/docs/vite.config.ts +++ b/packages/mermaid/src/docs/vite.config.ts @@ -36,6 +36,10 @@ export default defineConfig({ __dirname, '../../../mermaid-mindmap/dist/mermaid-mindmap.esm.min.mjs' ), // Use this one to build + '@mermaid-js/mermaid-timeline': path.join( + __dirname, + '../../../mermaid-timeline/dist/mermaid-timeline.esm.min.mjs' + ), }, }, server: {