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

[Docs] Improved EuiMarkdownEditor chart plugin example #3995

Merged
Merged
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
109 changes: 58 additions & 51 deletions src-docs/src/views/markdown_editor/markdown_editor_with_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import {
EuiButtonEmpty,
EuiForm,
EuiFormRow,
EuiSelect,
EuiColorPalettePicker,
EuiRange,
EuiText,
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';

import {
euiPaletteColorBlind,
euiPaletteComplimentary,
euiPaletteCool,
euiPaletteForStatus,
Expand All @@ -44,7 +45,6 @@ import {
} from '../../../../src/services/color';

const paletteData = {
euiPaletteColorBlind,
euiPaletteForStatus,
euiPaletteForTemperature,
euiPaletteComplimentary,
Expand All @@ -58,13 +58,13 @@ const paletteData = {
const paletteNames = Object.keys(paletteData);

const dg = new DataGenerator();
const data = dg.generateGroupedSeries(20, 5);
const generateData = categories => dg.generateGroupedSeries(10, categories);

const chartDemoPlugin = {
name: 'chartDemoPlugin',
button: {
label: 'Chart',
iconType: 'visArea',
iconType: 'visBarVerticalStacked',
},
helpText: (
<div>
Expand All @@ -76,64 +76,69 @@ const chartDemoPlugin = {
<p>Where options can contain:</p>
<ul>
<li>
<strong>palette: </strong>A number between 1-9 for each palette.
<strong>palette: </strong>A number between 1-8 for each palette.
</li>
<li>
<strong>height: </strong>
The height of the chart
<strong>categories: </strong>
The number of categories per column
</li>
</ul>
</EuiText>
</div>
),
editor: function ChartEditor({ node, onSave, onCancel }) {
const [palette, setPalette] = useState((node && node.palette) || 4);
const [height, setHeight] = useState((node && node.height) || 300);
const [palette, setPalette] = useState((node && node.palette) || '1');
const [categories, setCategories] = useState(5);

const onChange = e => {
setCategories(parseInt(e.target.value));
};

const palettes = paletteNames.map((paletteName, index) => {
return {
value: String(index + 1),
title: paletteName,
palette: paletteData[paletteNames[index]](categories),
type: 'fixed',
};
});

return (
<>
<EuiModalHeader>
<EuiModalHeaderTitle>Chart data</EuiModalHeaderTitle>
<EuiModalHeaderTitle>Add chart</EuiModalHeaderTitle>
</EuiModalHeader>

<EuiModalBody>
<>
<EuiForm>
<EuiFormRow label="Palette">
<EuiSelect
options={[
{ value: 4, text: 'red' },
{ value: 5, text: 'green' },
]}
value={palette}
onChange={e =>
setPalette(parseInt(e.currentTarget.value, 10))
}
/>
</EuiFormRow>

<EuiFormRow label="Height">
<EuiRange
value={height}
min={100}
max={500}
step={10}
showValue
valueAppend="px"
onChange={e => setHeight(parseInt(e.currentTarget.value, 10))}
/>
</EuiFormRow>
<EuiFlexGroup gutterSize="m" style={{ width: 600 }}>
<EuiFlexItem>
<EuiFormRow label="Palette">
<EuiColorPalettePicker
palettes={palettes}
onChange={setPalette}
value={palette}
compressed
/>
</EuiFormRow>
</EuiFlexItem>
<EuiFlexItem>
<EuiFormRow label="Categories">
<EuiRange
value={categories}
onChange={onChange}
min={1}
max={10}
compressed
showValue
/>
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>
</EuiForm>
<div
style={{
width: 600,
maxWidth: '100%',
height: 500,
alignItems: 'center',
display: 'flex',
}}>
<ChartMarkdownRenderer palette={palette} height={height} />
</div>
<EuiSpacer />
<ChartMarkdownRenderer palette={palette} categories={categories} />
</>
</EuiModalBody>

Expand All @@ -142,7 +147,7 @@ const chartDemoPlugin = {

<EuiButton
onClick={() =>
onSave(`!{chart${JSON.stringify({ palette, height })}}`, {
onSave(`!{chart${JSON.stringify({ palette, categories })}}`, {
block: true,
})
}
Expand Down Expand Up @@ -211,6 +216,7 @@ function ChartMarkdownParser() {
}

match += '}';

return eat(match)({
type: 'chartDemoPlugin',
...configuration,
Expand All @@ -221,14 +227,15 @@ function ChartMarkdownParser() {
methods.splice(methods.indexOf('text'), 0, 'chart');
}

const ChartMarkdownRenderer = ({ height = 200, palette = 5 }) => {
// receives the configuration from the parser and renders
const ChartMarkdownRenderer = ({ palette, categories }) => {
const customColors = {
colors: {
vizColors: paletteData[paletteNames[palette]](5),
vizColors: paletteData[paletteNames[palette - 1]](categories),
},
};
return (
<Chart size={{ height }}>
<Chart size={{ height: 320 }}>
<Settings
theme={[customColors, EUI_CHARTS_THEME_LIGHT]}
showLegend={false}
Expand All @@ -237,7 +244,7 @@ const ChartMarkdownRenderer = ({ height = 200, palette = 5 }) => {
<BarSeries
id="status"
name="Status"
data={data}
data={generateData(categories)}
xAccessor={'x'}
yAccessors={['y']}
splitSeriesAccessors={['g']}
Expand All @@ -261,7 +268,7 @@ Notice the toolbar above has a new chart button. Click it to add a chart.

Once you finish it'll add some syntax that looks like the below.

!{chart{"palette":4,"height":300}}
!{chart{"palette":"2","categories":5}}
`;

export default () => {
Expand Down