Highcharts in django

Highcharts in django

本文关键字:django in Highcharts      更新时间:2023-09-26

我是django的新手。我想在django中使用高图来绘制图表。我所做的是创建了一个名为Telecom的项目。然后我把highcharts文件放在Telecom/media/hightcharts文件夹中。我在Telecom/media/js位置写了一个script.js文件。

script.js

$(function () {
        $('#container').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: 'Monthly Average Rainfall'
            },
            subtitle: {
                text: 'Source: WorldClimate.com'
            },
            xAxis: {
                categories: [
                    'Jan',
                    'Feb',
                    'Mar',
                    'Apr',
                    'May',
                    'Jun',
                    'Jul',
                    'Aug',
                    'Sep',
                    'Oct',
                    'Nov',
                    'Dec'
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Rainfall (mm)'
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: [{
                name: 'Tokyo',
                data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
            }, {
                name: 'New York',
                data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]
            }, {
                name: 'London',
                data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]
            }, {
                name: 'Berlin',
                data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]
            }]
        });
    });

此外Telecom/templates/welcome/higraph.html

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script type = "text/javascript" src = "script.js"> 
        </script>
    </head>
    <body>
        <div id="container" style="width:50%; height:400px; float:right;">
        </div>
    </body>
</html>

设置.py

DEBUG = True
TEMPLATE_DEBUG = DEBUG
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(__file__)
ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'telecom_db',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '127.0.0.1',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
}
ALLOWED_HOSTS = []
TIME_ZONE = 'America/Chicago'
LANGUAGE_CODE = 'UTC'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'..', 'media')
MEDIA_URL = 'http://localhost:8000/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'b9_hyqe*b&ra_&wlm5a9xas_ag#5mjv-dy=to%hdk_u-#xvn*l'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#    'django.template.loaders.eggs.Loader',
)
TEMPLATE_DIRS = (       
                  os.path.join(PROJECT_DIR, '..','templates'),
#                  os.path.join(PROJECT_DIR, 'templates/welcome'),
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'Telecom.urls'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'welcome',
)
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

但是图形没有显示,如何使其工作?

您的程序需要互联网来显示图形,因为.js文件的位置来自url。如果您想离线加载图形,请下载所需的库(js文件)并进行如下更改。

settings.py#更新静态根和url

STATIC_ROOT = ''
STATIC_URL = '/static/'

现在将.js(源文件)放在静态文件夹中,该文件夹是在应用程序文件夹中创建的。

现在,在模板中,通过将下面的代码放入header标记来加载.js文件。

{% load staticfiles %}    
    <script src="{% static "js/jquery-1.3.2.min.js" %}"></script>
    <script src="{% static "js/script.js" %}"></script>

在这里,我把示例js库放在像/my_app/static/js/...这样的目录位置,所以在src上,你需要在静态文件夹之后给出位置。希望你在回答完这个问题后能够显示图表。

希望这将对您有所帮助:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SETTINGS_FILE_PATH = os.path.realpath(__file__)
SETTINGS_DIR = os.path.dirname(SETTINGS_FILE_PATH)
PROJECT_DIR = os.path.dirname(SETTINGS_DIR)
MEDIA_DIR = os.path.join(PROJECT_DIR, 'media')
STATIC_DIR = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    STATIC_DIR,
)
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_DIR

TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, 'templates'),
    )