Skip to content

Instantly share code, notes, and snippets.

@eonist
Created May 29, 2025 16:49
Show Gist options
  • Save eonist/3a2254d5ba8ae4562bcdcb0ca9a9258d to your computer and use it in GitHub Desktop.
Save eonist/3a2254d5ba8ae4562bcdcb0ca9a9258d to your computer and use it in GitHub Desktop.
bar.md

Based on the provided code structure and requirements, here's the implementation for the create_chart_component function:

/**
 * Creates a chart component with 5 bars distributed horizontally.
 * @param {string} parentId - The parent frame ID (green frame)
 * @returns {Promise} Test result with chart creation status
 */
async function create_chart_component(parentId) {
  console.log("💥 create_chart_component called with parentId:", parentId);
  
  // 1. Create the chart container frame
  const chartParams = {
    x: 0, y: 0,
    height: 100, // Fixed height
    name: "Chart Container",
    fillColor: { r: 1, g: 1, b: 1, a: 0 }, // Transparent
    parentId
  };
  
  const chartResult = await runStep({
    ws, channel,
    command: "create_frame",
    params: { frame: chartParams },
    assert: (response) => ({
      pass: Array.isArray(response.ids) && response.ids.length > 0,
      response
    }),
    label: "create_chart_container"
  });
  
  const chartId = chartResult.response?.ids?.[0];
  console.log("💥 Created chart container with ID:", chartId, "Result:", chartResult);
  if (!chartId) return chartResult;

  // 2. Set horizontal auto layout with 4px gap
  const chartLayoutParams = {
    layout: {
      nodeId: chartId,
      mode: "HORIZONTAL",
      layoutWrap: "NO_WRAP",
      itemSpacing: 4,
      primaryAxisSizing: "AUTO",
      counterAxisSizing: "AUTO"
    }
  };
  
  const chartLayoutResult = await runStep({
    ws, channel,
    command: "set_auto_layout",
    params: chartLayoutParams,
    assert: r => r && r["0"] && r["0"].success === true && r["0"].nodeId === chartId,
    label: "Set auto layout on chart container"
  });

  // 3. Set chart container to fill width
  const chartResizingResult = await runStep({
    ws, channel,
    command: "set_auto_layout_resizing",
    params: {
      nodeId: chartId,
      horizontal: "FILL",
      vertical: "AUTO"
    },
    assert: r => r && r.nodeId === chartId,
    label: "Set chart container to fill width"
  });

  // 4. Create the 5 bars
  const barConfigs = [
    { height: 20, opacity: 0.1, name: "Bar1" },
    { height: 35, opacity: 0.2, name: "Bar2" },
    { height: 50, opacity: 0.3, name: "Bar3" },
    { height: 70, opacity: 0.4, name: "Bar4" },
    { height: 90, opacity: 1.0, name: "Bar5" }
  ];

  const barResults = [];
  
  for (const config of barConfigs) {
    // Create rectangle for each bar
    const barParams = {
      x: 0, y: 0,
      width: 20, // Initial width, will be set to fill
      height: config.height,
      name: config.name,
      cornerRadius: 8,
      fillColor: { r: 0, g: 0, b: 0, a: config.opacity }, // Black with specified opacity
      parentId: chartId
    };
    
    const barResult = await runStep({
      ws, channel,
      command: "create_rectangle",
      params: { rectangle: barParams },
      assert: (response) => ({
        pass: Array.isArray(response.ids) && response.ids.length > 0,
        response
      }),
      label: `create_${config.name}`
    });
    
    const barId = barResult.response?.ids?.[0];
    console.log(`💥 Created ${config.name} with ID:`, barId, "Result:", barResult);
    
    if (barId) {
      // Set bar to fill width horizontally (similar to cash text)
      const barResizingResult = await runStep({
        ws, channel,
        command: "set_auto_layout_resizing",
        params: {
          nodeId: barId,
          horizontal: "FILL",
          vertical: "AUTO"
        },
        assert: r => r && r.nodeId === barId,
        label: `Set ${config.name} to fill width`
      });
      
      barResults.push({
        ...barResult,
        barResizingResult
      });
    } else {
      barResults.push(barResult);
    }
  }

  // Return all results for reporting
  return {
    ...chartResult,
    chartLayoutResult,
    chartResizingResult,
    barResults
  };
}

Key Implementation Details

Container Frame Setup:

  • Creates a frame with fixed height of 100px
  • Sets horizontal auto layout with 4px item spacing
  • Configures the container to fill the parent's width using set_auto_layout_resizing

Bar Creation:

  • Creates 5 rectangles with varying heights (20px to 90px)
  • Each bar has an 8px corner radius
  • Uses black fill color (#000000) with different opacity levels (10% to 100%)
  • Sets each bar to fill width horizontally using set_auto_layout_resizing

Auto Layout Configuration:

  • The container uses horizontal auto layout to distribute bars evenly
  • 4px gap between bars as specified
  • Each bar fills the available width equally due to the FILL horizontal resizing

This implementation follows the same patterns used in the existing code for the header and other components, ensuring consistency with the overall structure and approach[1].

Citations: [1] https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/attachments/60313916/ee5faa0e-2ac0-494f-aa53-92814ffe1d4a/paste.txt [2] https://help.figma.com/hc/en-us/articles/360040451373-Guide-to-auto-layout [3] https://stackoverflow.com/questions/61344676/how-to-create-a-see-through-rectangle-in-swiftui [4] https://www.youtube.com/watch?v=HwVuz8YS9T8 [5] https://www.youtube.com/watch?v=h9g3KUpFCOw [6] https://www.youtube.com/watch?v=0MwQ_se-t1o [7] https://help.figma.com/hc/en-us/articles/360039956914-Adjust-alignment-rotation-position-and-dimensions [8] https://thecodeaccelerator.com/blog/aligning-and-distributing-elements-in-figma [9] https://blog.prototypr.io/how-to-build-smart-resizable-charts-in-figma-using-auto-layout-6d55b7c726c7 [10] https://www.youtube.com/watch?v=LhlNkLAJ0qc [11] https://www.youtube.com/watch?v=TyaGpGDFczw [12] https://www.youtube.com/watch?v=JmccdyW4LiU [13] https://www.youtube.com/watch?v=c9uu-RNhZqI [14] https://goodpractices.design/tutorials/figma-auto-layout [15] https://lunacy.docs.icons8.com/autolayout/ [16] https://forum.figma.com/archive-21/proportional-auto-layout-width-height-and-max-min-8798 [17] https://developer.mozilla.org/en-US/docs/Web/CSS/fill-opacity [18] https://stackoverflow.com/questions/79073308/how-to-create-a-rectangle-in-swiftui-that-extends-width-according-to-the-number [19] https://vega.github.io/vega/docs/marks/rect/ [20] https://forum.figma.com/archive-21/how-to-distribute-objects-equally-spaced-29479 [21] https://forum.figma.com/archive-21/how-to-keep-things-equally-spaced-out-when-resizing-with-auto-layout-15157 [22] https://www.reddit.com/r/FigmaDesign/comments/18vab7y/figured_out_a_way_to_do_the_grids_of_charts_with/ [23] https://www.youtube.com/watch?v=aU0FXmVmW84 [24] https://www.figma.com/community/file/1295304364849245693/basic-charts [25] https://www.reddit.com/r/FigmaDesign/comments/16tyjry/autolayout_variable_horizontal_spacing/ [26] https://www.youtube.com/watch?v=tmLo7VK8wC8 [27] https://sjinnovation.com/how-to-use-auto-layout [28] https://www.youtube.com/watch?v=Hw0jaQDOUeg [29] https://www.w3schools.com/graphics/svg_rect.asp [30] https://blog.logrocket.com/create-fancy-corners-css/ [31] https://www.youtube.com/watch?v=q34kz4HuwTo [32] https://help.figma.com/hc/en-us/articles/360050986854-Adjust-corner-radius-and-smoothing [33] https://stackoverflow.com/questions/48784578/transparency-issues-drawing-a-rectangle-with-rounded-corners [34] https://forum.figma.com/suggest-a-feature-11/adding-auto-layout-should-keep-fill-resizing-in-children-24341 [35] https://www.reddit.com/r/FigmaDesign/comments/16g9qoz/is_there_no_way_to_set_a_component_to_fill/ [36] https://www.youtube.com/watch?v=fGxrLoxa30A [37] https://www.youtube.com/watch?v=fxGz3GaACRs [38] https://forum.figma.com/archive-21/issue-auto-layout-shows-fill-container-but-behave-as-fixed-size-26544 [39] https://www.youtube.com/watch?v=c3S5MR_LEYM [40] https://helpx.adobe.com/indesign/using/aligning-distributing-objects.html [41] https://forum.figma.com/ask-the-community-7/is-there-a-way-to-set-different-spacing-in-auto-layout-13872 [42] https://www.figma.com/community/plugin/1252800305273355573/auto-layout-bar-charts [43] https://www.figma.com/community/file/947304093865829310/stacked-bar-chart [44] https://www.youtube.com/watch?v=E3GIjXQtm24


Answer from Perplexity: pplx.ai/share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment