@yamada-ui/charts 2.0.0-next-20240706062355 → 2.0.0-next-20240716070106

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -2909,19 +2909,100 @@ var RadarChart = (0, import_core16.forwardRef)((props, ref) => {
2909
2909
  });
2910
2910
 
2911
2911
  // src/donut-chart.tsx
2912
- var import_core18 = require("@yamada-ui/core");
2913
- var import_utils19 = require("@yamada-ui/utils");
2912
+ var import_core19 = require("@yamada-ui/core");
2913
+ var import_utils20 = require("@yamada-ui/utils");
2914
2914
  var import_react16 = require("react");
2915
2915
  var import_recharts5 = require("recharts");
2916
2916
 
2917
2917
  // src/use-pie-chart.ts
2918
+ var import_core18 = require("@yamada-ui/core");
2919
+ var import_utils19 = require("@yamada-ui/utils");
2920
+ var import_react15 = require("react");
2921
+
2922
+ // src/pie-chart-label.tsx
2918
2923
  var import_core17 = require("@yamada-ui/core");
2919
2924
  var import_utils18 = require("@yamada-ui/utils");
2920
- var import_react15 = require("react");
2925
+ var import_jsx_runtime9 = require("react/jsx-runtime");
2926
+ var RADIAN = Math.PI / 180;
2927
+ var DEFAULT_LABEL_OFFSET = 22;
2928
+ var pieChartLabel = ({
2929
+ className: cellClassName,
2930
+ cx: cxProp = 0,
2931
+ cy: cyProp = 0,
2932
+ midAngle = 0,
2933
+ innerRadius = 0,
2934
+ outerRadius = 0,
2935
+ middleRadius = 0,
2936
+ percent = 0,
2937
+ value = 0,
2938
+ labelOffset: labelOffsetProp,
2939
+ isParcent,
2940
+ labelProps,
2941
+ valueFormatter,
2942
+ styles
2943
+ }) => {
2944
+ const labelOffset = labelOffsetProp != null ? labelOffsetProp : (outerRadius - innerRadius) * 0.5 + DEFAULT_LABEL_OFFSET;
2945
+ const x = cxProp + (middleRadius + labelOffset) * Math.cos(-midAngle * RADIAN);
2946
+ const y = cyProp + (middleRadius + labelOffset) * Math.sin(-midAngle * RADIAN);
2947
+ const textAnchor = x > cxProp ? "start" : x < cxProp ? "end" : "middle";
2948
+ const displayLabel = () => {
2949
+ if (isParcent) {
2950
+ return parseFloat((percent * 100).toFixed(0)) > 0 && `${(percent * 100).toFixed(0)}%`;
2951
+ } else if (!(0, import_utils18.isUndefined)(valueFormatter)) {
2952
+ return valueFormatter(value);
2953
+ } else {
2954
+ return value;
2955
+ }
2956
+ };
2957
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
2958
+ import_core17.ui.text,
2959
+ {
2960
+ className: (0, import_utils18.cx)(cellClassName, "ui-chart__label"),
2961
+ x,
2962
+ y,
2963
+ textAnchor,
2964
+ dominantBaseline: "central",
2965
+ __css: styles,
2966
+ ...labelProps,
2967
+ children: displayLabel()
2968
+ }
2969
+ );
2970
+ };
2971
+ var pieChartLabelLine = ({
2972
+ className: cellClassName,
2973
+ cx: cxProp = 0,
2974
+ cy: cyProp = 0,
2975
+ innerRadius = 0,
2976
+ midAngle = 0,
2977
+ middleRadius = 0,
2978
+ outerRadius = 0,
2979
+ points = [{ x: 0, y: 0 }],
2980
+ labelOffset: labelOffsetProp,
2981
+ labelLineProps,
2982
+ styles
2983
+ }) => {
2984
+ const labelOffset = labelOffsetProp != null ? labelOffsetProp : (outerRadius - innerRadius) * 0.5 + DEFAULT_LABEL_OFFSET;
2985
+ const x = cxProp + (middleRadius + labelOffset) * Math.cos(-midAngle * RADIAN);
2986
+ const y = cyProp + (middleRadius + labelOffset) * Math.sin(-midAngle * RADIAN);
2987
+ const d = `M ${points[0].x} ${points[0].y} L ${x} ${y}`;
2988
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
2989
+ import_core17.ui.path,
2990
+ {
2991
+ className: (0, import_utils18.cx)(cellClassName, "ui-chart__label-line"),
2992
+ d,
2993
+ __css: styles,
2994
+ ...labelLineProps
2995
+ }
2996
+ );
2997
+ };
2998
+
2999
+ // src/use-pie-chart.ts
2921
3000
  var usePieChart = ({
2922
3001
  data,
2923
3002
  withLabels = false,
2924
3003
  withLabelLines = false,
3004
+ labelOffset,
3005
+ isParcent = false,
2925
3006
  strokeWidth = 1,
2926
3007
  fillOpacity = 1,
2927
3008
  innerRadius = "0%",
@@ -2929,19 +3010,20 @@ var usePieChart = ({
2929
3010
  paddingAngle = 0,
2930
3011
  startAngle = 90,
2931
3012
  endAngle = -270,
3013
+ valueFormatter,
2932
3014
  styles,
2933
3015
  ...rest
2934
3016
  }) => {
2935
3017
  var _a, _b;
2936
- const { theme } = (0, import_core17.useTheme)();
3018
+ const { theme } = (0, import_core18.useTheme)();
2937
3019
  const [highlightedArea, setHighlightedArea] = (0, import_react15.useState)(null);
2938
3020
  const shouldHighlight = highlightedArea !== null;
2939
3021
  const { dimCell, ...computedCellProps } = (_a = rest.cellProps) != null ? _a : {};
2940
3022
  const {
2941
3023
  activeShape = {},
2942
3024
  inactiveShape = {},
2943
- label,
2944
- labelLine,
3025
+ label: labelProps,
3026
+ labelLine: labelLineProps,
2945
3027
  ...computedPieProps
2946
3028
  } = (_b = rest.pieProps) != null ? _b : {};
2947
3029
  const cellColors = (0, import_react15.useMemo)(
@@ -3003,13 +3085,27 @@ var usePieChart = ({
3003
3085
  )(theme, true),
3004
3086
  [inactiveShape, styles.inactiveShape, theme]
3005
3087
  );
3006
- const labelClassName = (0, import_react15.useMemo)(
3007
- () => getClassName({ ...styles.label, ...label })(theme),
3008
- [label, styles.label, theme]
3009
- );
3010
- const labelLineClassName = (0, import_react15.useMemo)(
3011
- () => getClassName({ ...styles.labelLine, ...labelLine })(theme),
3012
- [labelLine, styles.labelLine, theme]
3088
+ const label = (0, import_react15.useCallback)(
3089
+ (props) => pieChartLabel({
3090
+ labelOffset,
3091
+ isParcent,
3092
+ labelProps,
3093
+ valueFormatter,
3094
+ styles: styles.label,
3095
+ ...props
3096
+ }),
3097
+ [isParcent, labelOffset, labelProps, styles.label, valueFormatter]
3098
+ );
3099
+ const labelLine = (0, import_react15.useCallback)(
3100
+ (props) => {
3101
+ return pieChartLabelLine({
3102
+ labelOffset,
3103
+ labelLineProps,
3104
+ styles: styles.labelLine,
3105
+ ...props
3106
+ });
3107
+ },
3108
+ [labelLineProps, labelOffset, styles.labelLine]
3013
3109
  );
3014
3110
  const cellPropList = (0, import_react15.useMemo)(
3015
3111
  () => data.map((props, index) => {
@@ -3044,21 +3140,16 @@ var usePieChart = ({
3044
3140
  const getPieChartProps = (0, import_react15.useCallback)(
3045
3141
  ({ className, ...props } = {}, ref = null) => ({
3046
3142
  ref,
3047
- className: (0, import_utils18.cx)(className, chartClassName),
3143
+ className: (0, import_utils19.cx)(className, chartClassName),
3048
3144
  ...props,
3049
3145
  ...chartProps
3050
3146
  }),
3051
3147
  [chartProps, chartClassName]
3052
3148
  );
3053
3149
  const getPieProps = (0, import_react15.useCallback)(
3054
- ({
3055
- className,
3056
- labelClassName: labelClassNameProp,
3057
- labelLineClassName: labelLineClassNameProp,
3058
- ...props
3059
- }, ref = null) => ({
3150
+ ({ className, ...props }, ref = null) => ({
3060
3151
  ref,
3061
- className: (0, import_utils18.cx)(className, pieClassName),
3152
+ className: (0, import_utils19.cx)(className, pieClassName),
3062
3153
  dataKey: "value",
3063
3154
  data,
3064
3155
  rootTabIndex: -1,
@@ -3068,8 +3159,8 @@ var usePieChart = ({
3068
3159
  startAngle,
3069
3160
  endAngle,
3070
3161
  isAnimationActive: false,
3071
- label: withLabels ? { className: (0, import_utils18.cx)(labelClassNameProp, labelClassName) } : false,
3072
- labelLine: withLabelLines ? { className: (0, import_utils18.cx)(labelLineClassNameProp, labelLineClassName) } : false,
3162
+ label: withLabels ? label : false,
3163
+ labelLine: withLabelLines ? labelLine : false,
3073
3164
  activeShape: activeShapeProps,
3074
3165
  inactiveShape: inactiveShapeProps,
3075
3166
  ...props,
@@ -3084,9 +3175,9 @@ var usePieChart = ({
3084
3175
  startAngle,
3085
3176
  endAngle,
3086
3177
  withLabels,
3087
- labelClassName,
3178
+ label,
3088
3179
  withLabelLines,
3089
- labelLineClassName,
3180
+ labelLine,
3090
3181
  activeShapeProps,
3091
3182
  inactiveShapeProps,
3092
3183
  pieProps
@@ -3097,7 +3188,7 @@ var usePieChart = ({
3097
3188
  const { className, color } = cellPropList[index];
3098
3189
  return {
3099
3190
  ref,
3100
- className: (0, import_utils18.cx)(classNameProp, className),
3191
+ className: (0, import_utils19.cx)(classNameProp, className),
3101
3192
  fill: color,
3102
3193
  stroke: color,
3103
3194
  strokeWidth,
@@ -3116,9 +3207,9 @@ var usePieChart = ({
3116
3207
  };
3117
3208
 
3118
3209
  // src/donut-chart.tsx
3119
- var import_jsx_runtime9 = require("react/jsx-runtime");
3120
- var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3121
- const [styles, mergedProps] = (0, import_core18.useMultiComponentStyle)("DonutChart", props);
3210
+ var import_jsx_runtime10 = require("react/jsx-runtime");
3211
+ var DonutChart = (0, import_core19.forwardRef)((props, ref) => {
3212
+ const [styles, mergedProps] = (0, import_core19.useMultiComponentStyle)("DonutChart", props);
3122
3213
  const {
3123
3214
  className,
3124
3215
  data,
@@ -3138,12 +3229,14 @@ var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3138
3229
  endAngle,
3139
3230
  withLabels,
3140
3231
  withLabelLines,
3232
+ labelOffset,
3233
+ isParcent,
3141
3234
  innerRadius = withLabels ? "60%" : "80%",
3142
3235
  outerRadius,
3143
3236
  strokeWidth,
3144
3237
  legendProps,
3145
3238
  ...rest
3146
- } = (0, import_core18.omitThemeProps)(mergedProps);
3239
+ } = (0, import_core19.omitThemeProps)(mergedProps);
3147
3240
  const {
3148
3241
  pieVars,
3149
3242
  getPieProps,
@@ -3163,6 +3256,9 @@ var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3163
3256
  strokeWidth,
3164
3257
  withLabels,
3165
3258
  withLabelLines,
3259
+ labelOffset,
3260
+ isParcent,
3261
+ valueFormatter,
3166
3262
  styles
3167
3263
  });
3168
3264
  const { getContainerProps } = useChart({ containerProps });
@@ -3175,7 +3271,7 @@ var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3175
3271
  legendProps
3176
3272
  });
3177
3273
  const cells = (0, import_react16.useMemo)(
3178
- () => data.map(({ name }, index) => /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3274
+ () => data.map(({ name }, index) => /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3179
3275
  import_recharts5.Cell,
3180
3276
  {
3181
3277
  ...getCellProps({ index, className: "ui-donut-chart__cell" })
@@ -3184,38 +3280,36 @@ var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3184
3280
  )),
3185
3281
  [data, getCellProps]
3186
3282
  );
3187
- return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(ChartProvider, { value: { styles }, children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3188
- import_core18.ui.div,
3283
+ return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(ChartProvider, { value: { styles }, children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3284
+ import_core19.ui.div,
3189
3285
  {
3190
3286
  ref,
3191
- className: (0, import_utils19.cx)("ui-donut-chart", className),
3287
+ className: (0, import_utils20.cx)("ui-donut-chart", className),
3192
3288
  var: pieVars,
3193
3289
  __css: { ...styles.container },
3194
3290
  ...rest,
3195
- children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3291
+ children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3196
3292
  import_recharts5.ResponsiveContainer,
3197
3293
  {
3198
3294
  ...getContainerProps({ className: "ui-donut-chart__container" }),
3199
- children: /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
3295
+ children: /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(
3200
3296
  import_recharts5.PieChart,
3201
3297
  {
3202
3298
  ...getPieChartProps({ className: "ui-donut-chart__chart" }),
3203
3299
  children: [
3204
- /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3300
+ /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3205
3301
  import_recharts5.Pie,
3206
3302
  {
3207
3303
  ...getPieProps({
3208
- className: "ui-donut-chart__donut",
3209
- labelClassName: "ui-donut-chart__label",
3210
- labelLineClassName: "ui-donut-chart__label-line"
3304
+ className: "ui-donut-chart__donut"
3211
3305
  }),
3212
3306
  children: cells
3213
3307
  }
3214
3308
  ),
3215
- withLegend ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3309
+ withLegend ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3216
3310
  import_recharts5.Legend,
3217
3311
  {
3218
- content: ({ payload }) => /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3312
+ content: ({ payload }) => /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3219
3313
  ChartLegend,
3220
3314
  {
3221
3315
  className: "ui-donut-chart__legend",
@@ -3227,10 +3321,10 @@ var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3227
3321
  ...getLegendProps()
3228
3322
  }
3229
3323
  ) : null,
3230
- withTooltip ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3324
+ withTooltip ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3231
3325
  import_recharts5.Tooltip,
3232
3326
  {
3233
- content: ({ label, payload }) => /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
3327
+ content: ({ label, payload }) => /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3234
3328
  ChartTooltip,
3235
3329
  {
3236
3330
  className: "ui-donut-chart__tooltip",
@@ -3254,13 +3348,13 @@ var DonutChart = (0, import_core18.forwardRef)((props, ref) => {
3254
3348
  });
3255
3349
 
3256
3350
  // src/pie-chart.tsx
3257
- var import_core19 = require("@yamada-ui/core");
3258
- var import_utils20 = require("@yamada-ui/utils");
3351
+ var import_core20 = require("@yamada-ui/core");
3352
+ var import_utils21 = require("@yamada-ui/utils");
3259
3353
  var import_react17 = require("react");
3260
3354
  var import_recharts6 = require("recharts");
3261
- var import_jsx_runtime10 = require("react/jsx-runtime");
3262
- var PieChart = (0, import_core19.forwardRef)((props, ref) => {
3263
- const [styles, mergedProps] = (0, import_core19.useMultiComponentStyle)("PieChart", props);
3355
+ var import_jsx_runtime11 = require("react/jsx-runtime");
3356
+ var PieChart = (0, import_core20.forwardRef)((props, ref) => {
3357
+ const [styles, mergedProps] = (0, import_core20.useMultiComponentStyle)("PieChart", props);
3264
3358
  const {
3265
3359
  className,
3266
3360
  data,
@@ -3282,10 +3376,12 @@ var PieChart = (0, import_core19.forwardRef)((props, ref) => {
3282
3376
  endAngle,
3283
3377
  withLabels,
3284
3378
  withLabelLines,
3379
+ labelOffset,
3380
+ isParcent,
3285
3381
  strokeWidth,
3286
3382
  legendProps,
3287
3383
  ...rest
3288
- } = (0, import_core19.omitThemeProps)(mergedProps);
3384
+ } = (0, import_core20.omitThemeProps)(mergedProps);
3289
3385
  const {
3290
3386
  pieVars,
3291
3387
  getPieProps,
@@ -3305,6 +3401,9 @@ var PieChart = (0, import_core19.forwardRef)((props, ref) => {
3305
3401
  strokeWidth,
3306
3402
  withLabels,
3307
3403
  withLabelLines,
3404
+ labelOffset,
3405
+ isParcent,
3406
+ valueFormatter,
3308
3407
  styles
3309
3408
  });
3310
3409
  const { getContainerProps } = useChart({ containerProps });
@@ -3317,7 +3416,7 @@ var PieChart = (0, import_core19.forwardRef)((props, ref) => {
3317
3416
  legendProps
3318
3417
  });
3319
3418
  const cells = (0, import_react17.useMemo)(
3320
- () => data.map(({ name }, index) => /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3419
+ () => data.map(({ name }, index) => /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3321
3420
  import_recharts6.Cell,
3322
3421
  {
3323
3422
  ...getCellProps({ index, className: "ui-pie-chart__cell" })
@@ -3326,38 +3425,36 @@ var PieChart = (0, import_core19.forwardRef)((props, ref) => {
3326
3425
  )),
3327
3426
  [data, getCellProps]
3328
3427
  );
3329
- return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(ChartProvider, { value: { styles }, children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3330
- import_core19.ui.div,
3428
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(ChartProvider, { value: { styles }, children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3429
+ import_core20.ui.div,
3331
3430
  {
3332
3431
  ref,
3333
- className: (0, import_utils20.cx)("ui-pie-chart", className),
3432
+ className: (0, import_utils21.cx)("ui-pie-chart", className),
3334
3433
  var: pieVars,
3335
3434
  __css: { ...styles.container },
3336
3435
  ...rest,
3337
- children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3436
+ children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3338
3437
  import_recharts6.ResponsiveContainer,
3339
3438
  {
3340
3439
  ...getContainerProps({ className: "ui-pie-chart__container" }),
3341
- children: /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(
3440
+ children: /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(
3342
3441
  import_recharts6.PieChart,
3343
3442
  {
3344
3443
  ...getPieChartProps({ className: "ui-pie-chart__chart" }),
3345
3444
  children: [
3346
- /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3445
+ /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3347
3446
  import_recharts6.Pie,
3348
3447
  {
3349
3448
  ...getPieProps({
3350
- className: "ui-pie-chart__pie",
3351
- labelClassName: "ui-pie-chart__label",
3352
- labelLineClassName: "ui-pie-chart__label-line"
3449
+ className: "ui-pie-chart__pie"
3353
3450
  }),
3354
3451
  children: cells
3355
3452
  }
3356
3453
  ),
3357
- withLegend ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3454
+ withLegend ? /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3358
3455
  import_recharts6.Legend,
3359
3456
  {
3360
- content: ({ payload }) => /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3457
+ content: ({ payload }) => /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3361
3458
  ChartLegend,
3362
3459
  {
3363
3460
  className: "ui-pie-chart__legend",
@@ -3369,10 +3466,10 @@ var PieChart = (0, import_core19.forwardRef)((props, ref) => {
3369
3466
  ...getLegendProps()
3370
3467
  }
3371
3468
  ) : null,
3372
- withTooltip ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3469
+ withTooltip ? /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3373
3470
  import_recharts6.Tooltip,
3374
3471
  {
3375
- content: ({ label, payload }) => /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
3472
+ content: ({ label, payload }) => /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
3376
3473
  ChartTooltip,
3377
3474
  {
3378
3475
  className: "ui-pie-chart__tooltip",