Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / HTML, JavaScript, VBScript, CSS [игнор отключен] [закрыт для гостей] / Как передать значения из json в другую функцию / 5 сообщений из 5, страница 1 из 1
07.04.2014, 13:27
    #38607186
des1roer
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как передать значения из json в другую функцию
Есть такой код на JavaScript
Код: javascript
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
var plot;
 App = function(){
     var ajaxStack = [];
     this.pushAjaxResult = function(ajaxResult)
     {
         ajaxStack.push(ajaxResult);
     }
     this.getLastAjaxResult = function()
     {
         return ajaxStack[ajaxStack.length - 1];
     } };
 app = new App();
$(function () { 
    $.get('/testo.php',function(Arr){
        var x = JSON.parse(Arr);
        app.populateTable(x);
    }   ,"json")
    app.populateTable = function(x) {       
    var buf =[];
    for (var i = 0;i<x.length;i++){
        buf[i]=[i,x[i]];
     }
  
		plot = $.plot("#placeholder", [
			{ data: buf, label: "sin(x) = -0.00"}
		], {
			series: {
				lines: {
					show: true
				}
			},
			crosshair: {
				mode: "x"
			},
			grid: {
				hoverable: true,
				autoHighlight: false
			},
			yaxis: {
				min: 450,
				max: 600
			}
		});
}
		var legends = $("#placeholder .legendLabel");
		legends.each(function () {
			// fix the widths so they don't jump around
			$(this).css('width', $(this).width());
		});
		
		
		
		var updateLegendTimeout = null;
		var latestPosition = null;
		function updateLegend() {
			updateLegendTimeout = null;
			var pos = latestPosition;
			var axes = plot.getAxes();
			if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max ||
				pos.y < axes.yaxis.min || pos.y > axes.yaxis.max) {
				return;
			}


Он в принципе работает и отображает график (пользуюсь библиотекой http://www.flotcharts.org/ ) но вот беда - var axes = plot.getAxes(); не видит функцию plot и не отображает данные легенды
...
Рейтинг: 0 / 0
07.04.2014, 17:20
    #38607547
Малыхин Сергей
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как передать значения из json в другую функцию
Надеющийся_на_хрустальные_шары
Код: html
1.
2.
3.
4.
var plot; 
...
plot = $.plot( ...
...



Код не полный гадать по нему дело не благодарное ...
1. Что за функция $.plot() ? она должна возвращать объект у которого есть функция ".getAxes()" ?
2. В курсе что "var plot;" и "$.plot()" разные объекты?
...
Рейтинг: 0 / 0
08.04.2014, 06:03
    #38607841
des1roer
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как передать значения из json в другую функцию
Вот полный код
Код: javascript
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples</title>
    <link href="layout.css" rel="stylesheet" type="text/css"></link>
    <!--[if IE]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="../jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.flot.crosshair.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

    <div id="placeholder" style="width:600px;height:300px"></div>

    <p>You can add crosshairs that'll track the mouse position, either
    on both axes or as here on only one.</p>

    <p>If you combine it with listening on hover events, you can use
    it to track the intersection on the curves by interpolating
    the data points (look at the legend).</p>

    <p id="hoverdata"></p>

<script id="source" language="javascript" type="text/javascript">


var plot;
 App = function(){
     var ajaxStack = [];
     this.pushAjaxResult = function(ajaxResult)
     {
         ajaxStack.push(ajaxResult);
     }
     this.getLastAjaxResult = function()
     {
         return ajaxStack[ajaxStack.length - 1];
     } };
 app = new App();
$(function () { 
    $.get('/testo.php',function(Arr){
        var x = JSON.parse(Arr);
        app.populateTable(x);
    }   ,"json")
app.populateTable = function(x) {       
    var buf =[];
    for (var i = 0;i<x.length;i++){
        buf[i]=[i,x[i]];
     }
  
    plot = $.plot($("#placeholder"),
                      [ 
                        { data: buf, label: "cos(x) = -0.00" } ], {
                            series: {
                                lines: { show: true }
                            },
                            crosshair: { mode: "x" },
                            grid: { hoverable: true, autoHighlight: false },
                            yaxis: { min: 450, max: 600 }
                        });
						
    var legends = $("#placeholder .legendLabel");
    legends.each(function () {
        // fix the widths so they don't jump around
        $(this).css('width', $(this).width());
    });

	
    var updateLegendTimeout = null;
    var latestPosition = null;
    
    function updateLegend() {
        updateLegendTimeout = null;
        
        var pos = latestPosition;
        
        var axes = plot.getAxes();
        if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max ||
            pos.y < axes.yaxis.min || pos.y > axes.yaxis.max)
            return;

        var i, j, dataset = plot.getData();
        for (i = 0; i < dataset.length; ++i) {
            var series = dataset[i];

            // find the nearest points, x-wise
            for (j = 0; j < series.data.length; ++j)
                if (series.data[j][0] > pos.x)
                    break;
            
            // now interpolate
            var y, p1 = series.data[j - 1], p2 = series.data[j];
            if (p1 == null)
                y = p2[1];
            else if (p2 == null)
                y = p1[1];
            else
                y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);

            legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2)));
        }
    }
    
    $("#placeholder").bind("plothover",  function (event, pos, item) {
        latestPosition = pos;
        if (!updateLegendTimeout)
            updateLegendTimeout = setTimeout(updateLegend, 50);
    });
	};
});

</script>

 </body>
</html>
...
Рейтинг: 0 / 0
08.04.2014, 06:04
    #38607842
des1roer
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как передать значения из json в другую функцию
Дело в том, что функция plot рисует график, но я не могу к ней позже обратиться и получить от неё значения положения курсора
...
Рейтинг: 0 / 0
08.04.2014, 13:29
    #38608409
Малыхин Сергей
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как передать значения из json в другую функцию
Вот полный код
Код не полный и запустить его невозможно
...
Рейтинг: 0 / 0
Форумы / HTML, JavaScript, VBScript, CSS [игнор отключен] [закрыт для гостей] / Как передать значения из json в другую функцию / 5 сообщений из 5, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]