You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dash框架图表X轴月份显示不一致问题及跨屏幕自动适配方案咨询

Fixing Responsive X-axis Display for Dash Combo Charts

Hey there! Let's sort out that inconsistent X-axis display issue you're seeing across different machines. The problem where some devices show a zoomed-in subset of months instead of the full range is almost certainly due to how Plotly handles layout scaling based on screen resolution. Here are targeted fixes you can apply to ensure your chart adapts smoothly to any screen size:

1. Force X-axis to Show All Month Categories

First, make sure Plotly treats your ready_year_month values as discrete categories instead of continuous data. This prevents it from automatically zooming or skipping ticks on smaller screens. Add these settings to your xaxis configuration in the layout:

figure = { 
    'data': [{
        'x' : direct_closed['ready_year_month'],
        'y' : direct_closed['C_Number'],
        'type': 'bar',
        'name': 'Direct Count'
    }, {
        'x' : direct_closed['ready_year_month'],
        'y' : direct_closed['closure_days'],
        'type': 'line',
        'name': 'Avg Closure Time'
    } ],
    'layout': {
        'margin' : {'l': 40, 't': 40, 'r': 10, 'b': 60},  # Increased bottom margin to fit rotated ticks
        "title" : {"text": "<b>{} Direct Closed in {} area(s)</b>".format(closed_count, area_input)},
        'xaxis': {
            'type': 'category',  # Treat months as discrete, non-numeric categories
            'tickmode': 'linear',  # Ensure every single month tick is displayed
            'autorange': True,  # Auto-fit to include all month categories
            'tickangle': -45  # Rotate ticks to avoid overlap on narrow screens
        },
        'autosize': True,  # Let the chart resize with its container
        'responsive': True  # Enable core responsive layout behavior
    }
}

2. Ensure the Chart Container is Responsive

Your outer html.Div and dcc.Graph components need to inherit proper responsive sizing. Update the dcc.Graph element to use 100% width, so it scales with its parent container:

html.Div(
    className = "row",
    children = [
        html.Div(
            className = 'eight columns plot-settings',
            children = [
                dcc.Graph(
                    id = "sp-direct-graph",
                    figure=figure,
                    style={'width': '100%', 'height': '600px'}  # Responsive width, fixed height (use '100%' for dynamic height)
                ),
            ],
        ),
    ],
)

3. Adjust CSS for Consistent Container Sizing

If you're using a framework like Bootstrap (implied by the eight columns class), double-check that the plot-settings class doesn't have fixed width constraints. Add this to your app's CSS to ensure the container expands/shrinks properly:

.plot-settings {
    width: 100% !important;
    padding: 0 15px;  # Optional: Prevent edge clipping on small screens
}

Why These Fixes Work:

  • xaxis.type='category' tells Plotly to treat each month as a unique item, so it won't skip or zoom into ticks like it would with continuous numeric data.
  • tickmode='linear' guarantees every month is displayed, while rotated ticks prevent overlap on narrow screens.
  • autosize=True and responsive container styles let the chart adjust its width dynamically, while still preserving the full X-axis dataset.

Give these changes a try—they should resolve the inconsistent display across different machines!

内容的提问来源于stack exchange,提问作者Vijay Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 07:57:30