Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | 3x 3x 3x 189x 3x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 6x 6x 24x 3x 3x 3x 3x 3x 3x 3x 3x | import _ from 'lodash'; import React, { Key } from 'react'; import PropTypes from 'prop-types'; import * as d3Scale from 'd3-scale'; import * as chartConstants from '../../constants/charts'; import { lucidClassNames } from '../../util/style-helpers'; import { getFirst, StandardProps } from '../../util/component-types'; import { Collection, maxByFields, maxByFieldsStacked, } from '../../util/chart-helpers'; import Axis from '../Axis/Axis'; import AxisLabel from '../AxisLabel/AxisLabel'; import Bars from '../Bars/Bars'; import ContextMenu from '../ContextMenu/ContextMenu'; import Legend from '../Legend/Legend'; import EmptyStateWrapper from '../EmptyStateWrapper/EmptyStateWrapper'; const cx = lucidClassNames.bind('&-BarChart'); const { arrayOf, func, number, object, shape, string, array, bool, oneOfType, oneOf, } = PropTypes; export interface IBarChartProps extends StandardProps, React.DetailedHTMLProps< React.HTMLAttributes<HTMLDivElement>, HTMLDivElement > { /** Child components of LineChart */ EmptyStateWrapper?: React.ReactNode; /** * Height of the chart. */ height: number; /** Width of the chart. */ width: number; /** An object defining the margins of the chart. These margins typically * contain the axis and labels. */ margin: { top: number; right: number; bottom: number; left: number; }; /** Data for the chart. E.g. * [ * { x: 'Monday' , y: 1 } , * { x: 'Tuesday' , y: 2 } , * { x: 'Wednesday' , y: 3 } , * { x: 'Thursday' , y: 2 } , * { x: 'Friday' , y: 5 } , * ] */ data: Collection; /** An object with human readable names for fields that will be used for legends and tooltips. E.g: * { * x: 'Date', * y: 'Impressions', * } */ legend?: {}; /** Controls the visibility of the \`LoadingMessage\`. */ isLoading?: boolean; /** Show tool tips on hover. */ hasToolTips: boolean; /** Show a legend at the bottom of the chart. */ hasLegend: boolean; /** Takes one of the palettes exported from \`lucid.chartConstants\`. Available palettes: * - \`PALETTE_7\` (default) * - \`PALETTE_30\` * - \`PALETTE_MONOCHROME_0_5\` * - \`PALETTE_MONOCHROME_1_5\` * - \`PALETTE_MONOCHROME_2_5\` * - \`PALETTE_MONOCHROME_3_5\` * - \`PALETTE_MONOCHROME_4_5\` * - \`PALETTE_MONOCHROME_5_5\` * - \`PALETTE_MONOCHROME_6_5\` */ palette: string[]; /** You can pass in an object if you want to map x values to \`lucid.chartConstants\` or custom colors: * { * 'imps': COLOR_0, * 'rev': COLOR_3, * 'clicks': '#abc123', * } */ colorMap?: {}; /** The field we should look up your x data by. Your actual x data must be * strings. */ xAxisField: string; /** There are some cases where you need to only show a "sampling" of ticks on * the x axis. This number will control that. */ xAxisTickCount: number | null; /** An optional function used to format your x axis data. If you don't * provide anything, we'll use an identity function. */ xAxisFormatter: (d: number | Date) => string; /** Set a title for the x axis. */ xAxisTitle: string | null; /** Set a color for the x axis title. Use the color constants exported off \`lucid.chartConstants\`. E.g.: * - \`COLOR_0\` * - \`COLOR_GOOD\` * - \`'#123abc'\` * custom color hex * \`number\` is supported only for backwards compatability. */ xAxisTitleColor: string | number; /** An array of your y axis fields. Typically this will just be a single item * unless you need to display grouped or stacked bars. The order of the * array determines the series order in the chart. */ yAxisFields: string[]; /** The minimum number the y axis should display. Typically this should be be * \`0\`. */ yAxisMin: number; /** The maximum number the y axis should display. This should almost always * be the largest number from your dataset. */ yAxisMax?: number; /** An optional function used to format your y axis data. If you don't * provide anything, we use the default D3 number formatter. */ yAxisFormatter?: (v: unknown) => string; /** Stack the y axis data instead of showing it as groups. This is only * useful if you have multiple \`yAxisFields\`. Stacking will cause the * chart to be aggregated by sum. */ yAxisIsStacked: boolean; /** There are some cases where you need to only show a "sampling" of ticks on * the y axis. This number will control that. */ yAxisTickCount: number | null; /** Set a title for the y axis. */ yAxisTitle: string | null; /** Set a color for the y axis title. Use the color constants exported off \`lucid.chartConstants\`. E.g.: * - \`COLOR_0\` * - \`COLOR_GOOD\` * - \`'#123abc'\` * custom color hex * \`number\` is supported only for backwards compatability. */ yAxisTitleColor: number | string; /** An optional function used to format your y axis titles and data in the * tooltip legends. The first value is the name of your y field, the second value * is your post-formatted y value, and the third value is your non-formatted * y-value. Signature: \`(yField, yValueFormatted, yValue) => {}\` */ yAxisTooltipFormatter: ( yField: string, yValueFormatted: Key, yValue: number ) => Key; /** An optional function used to format y-values in the tooltip legends. */ yAxisTooltipDataFormatter?: (d: number | Date) => string; /** An optional function used to format the entire tooltip body. The only arg is * the associated data point. This formatter will over-ride yAxisTooltipFormatter * and yAxisTooltipDataFormatter. Signature: * \`dataPoint => {}\` */ renderTooltipBody: (dataPoint: string | number | object) => {}; /** Determines the orientation of the tick text. This may override what the orient prop * tries to determine. */ xAxisTextOrientation: 'vertical' | 'horizontal' | 'diagonal'; /** Determines the orientation of the tick text. This may override what the orient prop * tries to determine. */ yAxisTextOrientation: 'vertical' | 'horizontal' | 'diagonal'; } const defaultProps = { height: 400, width: 1000, // duplicated because `statics` aren't available during getDefaultProps margin: { top: 10, right: 20, bottom: 50, left: 80, }, palette: chartConstants.PALETTE_7, hasToolTips: true, hasLegend: false, renderTooltipBody: null, xAxisField: 'x', xAxisTickCount: null, xAxisTitle: null, xAxisTitleColor: '#000', xAxisFormatter: _.identity, xAxisTextOrientation: 'horizontal', yAxisFields: ['y'], yAxisTickCount: null, yAxisIsStacked: false, yAxisMin: 0, yAxisTitle: null, yAxisTitleColor: '#000', yAxisTooltipFormatter: (yField: unknown, yValueFormatted: unknown) => `${yField}: ${yValueFormatted}`, yAxisTextOrientation: 'horizontal', }; export const BarChart = (props: IBarChartProps): React.ReactElement => { const { className, height, width, margin: marginOriginal, data, legend, isLoading, hasToolTips, hasLegend, palette, colorMap, renderTooltipBody, xAxisField, xAxisFormatter, xAxisTitle, xAxisTitleColor, xAxisTickCount, xAxisTextOrientation, yAxisFields, yAxisFormatter, yAxisTitle, yAxisTitleColor, yAxisIsStacked, yAxisTickCount, yAxisMin, yAxisTooltipFormatter, yAxisTooltipDataFormatter, yAxisMax = yAxisIsStacked ? maxByFieldsStacked(data, yAxisFields) : maxByFields(data, yAxisFields), yAxisTextOrientation, ...passThroughs } = props; const margin = { ...BarChart.MARGIN, ...marginOriginal, }; const svgClasses = cx(className, '&'); const innerWidth = width - margin.left - margin.right; const innerHeight = height - margin.top - margin.bottom; // `paddingInner` determines the space between the bars or groups of bars const paddingInner = yAxisFields.length > 1 ? BarChart.PADDING_GROUPED_OR_STACKED : BarChart.PADDING; const xScale = d3Scale .scaleBand() .domain(_.map(data, xAxisField)) .range([0, innerWidth]) .paddingInner(paddingInner) .paddingOuter(0.5); const yScale = d3Scale .scaleLinear() .domain([yAxisMin, yAxisMax as any]) .range([innerHeight, 0]); // @ts-ignore const xAxisFinalFormatter = xAxisFormatter || xScale.tickFormat(); const yAxisFinalFormatter = yAxisFormatter || yScale.tickFormat(); const yFinalFormatter = yAxisTooltipDataFormatter ? yAxisTooltipDataFormatter : yAxisFinalFormatter; if (_.isEmpty(data) || width < 1 || height < 1 || isLoading) { const emptyStateWrapper: any = getFirst( props, BarChart.EmptyStateWrapper ) || <BarChart.EmptyStateWrapper Title='You have no data.' />; return ( <EmptyStateWrapper {...emptyStateWrapper.props} isEmpty={_.isEmpty(data)} isLoading={isLoading} > {emptyStateWrapper.props.children} <svg {...(passThroughs as any)} className={svgClasses} width={width} height={height} > {/* x axis */} <g transform={`translate(${margin.left}, ${innerHeight + margin.top})`} > <Axis orient='bottom' scale={xScale as any} tickCount={xAxisTickCount} /> </g> {/* y axis */} <g transform={`translate(${margin.left}, ${margin.top})`}> <Axis orient='left' scale={yScale} tickFormat={yFinalFormatter} tickCount={yAxisTickCount} /> </g> </svg> </EmptyStateWrapper> ); } return ( <svg {...(passThroughs as any)} className={svgClasses} width={width} height={height} > {/* x axis */} <g transform={`translate(${margin.left}, ${innerHeight + margin.top})`}> <Axis orient='bottom' // @ts-ignore scale={xScale} outerTickSize={0} tickFormat={xAxisFinalFormatter} tickCount={xAxisTickCount} textOrientation={xAxisTextOrientation} /> {hasLegend ? ( <ContextMenu direction='down' alignment='center' directonOffset={ (margin.bottom / 2 + Legend.HEIGHT / 2) * -1 /* should center the legend in the bottom margin */ } > <ContextMenu.Target elementType='g'> <rect className={cx('&-invisible')} width={innerWidth} height={margin.bottom} /> </ContextMenu.Target> <ContextMenu.FlyOut className={cx('&-legend-container')}> <Legend orient='horizontal'> {_.map(yAxisFields, (field, index) => ( <Legend.Item key={index} hasPoint={true} hasLine={false} color={_.get( colorMap, field, palette[index % palette.length] )} pointKind={1} > {_.get(legend, field, field)} </Legend.Item> ))} </Legend> </ContextMenu.FlyOut> </ContextMenu> ) : null} </g> {/* x axis title */} {xAxisTitle ? ( <g transform={`translate(${margin.left}, ${margin.top + innerHeight})`}> <AxisLabel orient='bottom' width={innerWidth} height={margin.bottom} label={xAxisTitle} color={ _.isString(xAxisTitleColor) ? xAxisTitleColor : palette[xAxisTitleColor % palette.length] } /> </g> ) : null} {/* y axis */} <g transform={`translate(${margin.left}, ${margin.top})`}> <Axis orient='left' scale={yScale} tickFormat={yAxisFinalFormatter} tickCount={yAxisTickCount} textOrientation={yAxisTextOrientation} /> </g> {/* y axis title */} {yAxisTitle ? ( <g transform={`translate(0, ${margin.top})`}> <AxisLabel orient='left' width={margin.left} height={innerHeight} label={yAxisTitle} color={ _.isString(yAxisTitleColor) ? yAxisTitleColor : palette[yAxisTitleColor % palette.length] } /> </g> ) : null} {/* bars */} <g transform={`translate(${margin.left}, ${margin.top})`}> <Bars xField={xAxisField} xScale={xScale} xFormatter={xAxisFormatter} yFields={yAxisFields} yScale={yScale} // @ts-ignore yFormatter={yFinalFormatter} yStackedMax={yAxisMax as any} data={data} isStacked={yAxisIsStacked} yTooltipFormatter={yAxisTooltipFormatter} hasToolTips={hasToolTips} legend={legend} palette={palette} colorMap={colorMap} renderTooltipBody={renderTooltipBody} /> </g> </svg> ); }; BarChart.displayName = 'BarChart'; BarChart.propTypes = { /** Appended to the component-specific class names set on the root element. */ className: string, /** Height of the chart. */ height: number, /** Width of the chart. */ width: number, /** An object defining the margins of the chart. These margins typically contain the axis and labels. */ margin: shape({ top: number, right: number, bottom: number, left: number, }), /** Data for the chart. E.g. [ { x: 'Monday' , y: 1 } , { x: 'Tuesday' , y: 2 } , { x: 'Wednesday' , y: 3 } , { x: 'Thursday' , y: 2 } , { x: 'Friday' , y: 5 } , ] */ data: arrayOf(object), /** An object with human readable names for fields that will be used for legends and tooltips. E.g: { x: 'Date', y: 'Impressions', } */ legend: object, /** Controls the visibility of the \`LoadingMessage\`. */ isLoading: bool, /** Show tool tips on hover. */ hasToolTips: bool, /** Show a legend at the bottom of the chart. */ hasLegend: bool, /** Takes one of the palettes exported from \`lucid.chartConstants\`. Available palettes: - \`PALETTE_7\` (default) - \`PALETTE_30\` - \`PALETTE_MONOCHROME_0_5\` - \`PALETTE_MONOCHROME_1_5\` - \`PALETTE_MONOCHROME_2_5\` - \`PALETTE_MONOCHROME_3_5\` - \`PALETTE_MONOCHROME_4_5\` - \`PALETTE_MONOCHROME_5_5\` - \`PALETTE_MONOCHROME_6_5\` */ palette: arrayOf(string), /** You can pass in an object if you want to map x values to \`lucid.chartConstants\` or custom colors: { 'imps': COLOR_0, 'rev': COLOR_3, 'clicks': '#abc123', } */ colorMap: object, /** The field we should look up your x data by. Your actual x data must be strings. */ xAxisField: string, /** There are some cases where you need to only show a "sampling" of ticks on the x axis. This number will control that. */ xAxisTickCount: number, /** An optional function used to format your x axis data. If you don't provide anything, we'll use an identity function. */ xAxisFormatter: func, /** Set a title for the x axis. */ xAxisTitle: string, /** Set a color for the x axis title. Use the color constants exported off \`lucid.chartConstants\`. E.g.: - \`COLOR_0\` - \`COLOR_GOOD\` - \`'#123abc'\` // custom color hex \`number\` is supported only for backwards compatability. */ xAxisTitleColor: oneOfType([number, string]), /** An array of your y axis fields. Typically this will just be a single item unless you need to display grouped or stacked bars. The order of the array determines the series order in the chart. */ yAxisFields: array, /** The minimum number the y axis should display. Typically this should be be \`0\`. */ yAxisMin: number, /** The maximum number the y axis should display. This should almost always be the largest number from your dataset. */ yAxisMax: number, /** An optional function used to format your y axis data. If you don't provide anything, we use the default D3 number formatter. */ yAxisFormatter: func, /** Stack the y axis data instead of showing it as groups. This is only useful if you have multiple \`yAxisFields\`. Stacking will cause the chart to be aggregated by sum. */ yAxisIsStacked: bool, /** There are some cases where you need to only show a "sampling" of ticks on the y axis. This number will control that. */ yAxisTickCount: number, /** Set a title for the y axis. */ yAxisTitle: string, /** Set a color for the y axis title. Use the color constants exported off \`lucid.chartConstants\`. E.g.: - \`COLOR_0\` - \`COLOR_GOOD\` - \`'#123abc'\` // custom color hex \`number\` is supported only for backwards compatability. */ yAxisTitleColor: oneOfType([number, string]), /** An optional function used to format your y axis titles and data in the tooltip legends. The first value is the name of your y field, the second value is your post-formatted y value, and the third value is your non-formatted y-value. Signature: \`(yField, yValueFormatted, yValue) => {}\` */ yAxisTooltipFormatter: func, /** An optional function used to format y-values in the tooltip legends. */ yAxisTooltipDataFormatter: func, /** An optional function used to format the entire tooltip body. The only arg is the associated data point. This formatter will over-ride yAxisTooltipFormatter and yAxisTooltipDataFormatter. Signature: \`dataPoint => {}\` */ renderTooltipBody: func, /** Determines the orientation of the tick text. This may override what the orient prop tries to determine. */ xAxisTextOrientation: oneOf(['vertical', 'horizontal', 'diagonal']), /** Determines the orientation of the tick text. This may override what the orient prop tries to determine. */ yAxisTextOrientation: oneOf(['vertical', 'horizontal', 'diagonal']), }; BarChart.defaultProps = defaultProps; BarChart.peek = { description: `A \`Bar Chart\` is great for showing data that fits neatly into "buckets". The x axis data must be strings, and the y axis data must be numeric.`, categories: ['visualizations', 'charts'], madeFrom: ['ContextMenu', 'ToolTip'], }; BarChart.EmptyStateWrapper = EmptyStateWrapper; BarChart.PADDING = 0.05; BarChart.PADDING_GROUPED_OR_STACKED = 0.3; BarChart.MARGIN = { top: 10, right: 20, bottom: 50, left: 80, }; export default BarChart; |