Google Charts:Gauge-Graph在秒后更改为最大值

时间:2017-01-08 14:14:17

标签: javascript graph charts google-visualization gauge

我正在使用Google Charts API开展一个小型学生项目(从Raspberry Pi向服务器发送温度)。在我的服务器的HTML响应中,返回一个规范图。一切都运行正常,除了一件小事:大约10秒钟后,两个仪表中的第一个跳转到其最大值(一个值为高,这不是数据集的一部分)。

在另一个项目中,我已经遇到了同样的错误(有4个图表)。奇怪的是,它始终只发生在第一个规格图上。

你可以在http://141.28.105.163/records下自己看到......大约10秒后,第一张图从大约21跳到51或类似的东西。

这是代码:

<html>
<head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
        google.charts.load('current', {'packages':['gauge']});
        google.charts.setOnLoadCallback(drawChart);
        function drawChart() {

            var data = google.visualization.arrayToDataTable([
                ['Label', 'Value'],
                ['Min', 17],
                ['Max', 23]
            ]);

            var options = {
                minorTicks: 10,
                min: 0,
                max: 40
            };

            var chart = new google.visualization.Gauge(document.getElementById('temp_chart'));

            setInterval(function() {
                data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
                chart.draw(data, options);
            }, 13000);

            chart.draw(data, options);
      }
    </script>
</head>
<body>
    <div id="temp_chart" style="width: 100%; height: 100%"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

谢谢,就是这样。我只是从Google复制和粘贴的整个部分:

#include <Windows.h>

#include <d2d1_1.h> 
#pragma comment(lib,"d2d1")

ID2D1Factory * d2factory_ptr = NULL;
ID2D1HwndRenderTarget * renderTarget_ptr = NULL;


LRESULT CALLBACK mainWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(
    HINSTANCE hInstance
    , HINSTANCE prevInstance
    , LPWSTR cmd
    , int nCmdShow
) {
    WNDCLASSEX wndClassStruct;
    ZeroMemory(&wndClassStruct, sizeof(WNDCLASSEX));
    wndClassStruct.cbSize = sizeof(WNDCLASSEX);
    wndClassStruct.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wndClassStruct.style = CS_HREDRAW | CS_VREDRAW;
    wndClassStruct.hInstance = hInstance;
    wndClassStruct.lpfnWndProc = mainWinProc;
    wndClassStruct.lpszClassName = TEXT("MainWnd");

    RegisterClassEx(&wndClassStruct);

    RECT windowRect = { 0,0,640,480};
    AdjustWindowRectEx(&windowRect, WS_OVERLAPPEDWINDOW, 0, WS_EX_APPWINDOW);
    HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, TEXT("MainWnd"), TEXT("Direct 2D Test Window"), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, CW_USEDEFAULT, 0, windowRect.right-windowRect.left, windowRect.bottom-windowRect.top, NULL, NULL, hInstance, 0);

    {
        D2D1_FACTORY_OPTIONS fo;
        ZeroMemory(&fo, sizeof(D2D1_FACTORY_OPTIONS));

        IID const factoryIID = IID_ID2D1Factory1;

        HRESULT res = S_OK;
        if (S_OK != (res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factoryIID, &fo, &d2factory_ptr))) {
            return 0;
        }

        RECT clientRect;
        GetClientRect(hWnd, &clientRect);

        D2D1_RENDER_TARGET_PROPERTIES renderTargetProps;
        ZeroMemory(&renderTargetProps, sizeof(D2D1_RENDER_TARGET_PROPERTIES));
        renderTargetProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
        renderTargetProps.pixelFormat = (D2D1_PIXEL_FORMAT) { DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED };
        renderTargetProps.dpiX = 0;
        renderTargetProps.dpiY = 0;
        renderTargetProps.usage = D2D1_RENDER_TARGET_USAGE_FORCE_BITMAP_REMOTING;
        renderTargetProps.minLevel = D2D1_FEATURE_LEVEL_DEFAULT;

        D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderProps;
        ZeroMemory(&hwndRenderProps, sizeof(D2D1_HWND_RENDER_TARGET_PROPERTIES));
        hwndRenderProps.hwnd = hWnd;
        hwndRenderProps.pixelSize = (D2D1_SIZE_U) { clientRect.right - clientRect.left, clientRect.bottom - clientRect.top };
        hwndRenderProps.presentOptions = D2D1_PRESENT_OPTIONS_NONE;

        if (S_OK != (res = d2factory_ptr->lpVtbl->CreateHwndRenderTarget(d2factory_ptr, &renderTargetProps, &hwndRenderProps, &renderTarget_ptr))) {
            return 0;
        }
    }

    ShowWindow(hWnd, nCmdShow);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    DestroyWindow(hWnd);

    if(NULL != renderTarget_ptr) 
        renderTarget_ptr->lpVtbl->Base.Base.Base.Release((IUnknown*)renderTarget_ptr);

    if (NULL != d2factory_ptr)
        d2factory_ptr->lpVtbl->Base.Release((IUnknown*)d2factory_ptr);

    return 0;
}

LRESULT onPaintMainWindow() {
    ID2D1RenderTargetVtbl renderTargetFuncs = renderTarget_ptr->lpVtbl->Base;
    ID2D1RenderTarget * This = (ID2D1RenderTarget*)renderTarget_ptr;
    D2D1_TAG tag1, tag2;

    D2D1_COLOR_F backgroundClr = (D2D1_COLOR_F) { 0.0, 0.5, 1.0, 1.0 };
    renderTargetFuncs.BeginDraw(This);
    renderTargetFuncs.Clear(This, &backgroundClr);
    renderTargetFuncs.EndDraw(This, &tag1, &tag2);

    return 0;
}

LRESULT CALLBACK mainWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

    if (WM_PAINT == uMsg)
        return onPaintMainWindow();

    if (WM_DESTROY == uMsg) {
        PostQuitMessage(0); return 0;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

没有它就能完美运作!