comparison scripts/plot/appearance/legend.m @ 20443:71bd638e0ef6

legend.m: Fix listeners on legend line objects (bug #45563). * legend.m: Get list of hplots already in the existing legend (old_hplots). Add listeners on new hplot objects when they are not in the list old_hplots. Re-ordered %!demo blocks to make slightly more logical sense.
author Rik <rik@octave.org>
date Wed, 29 Jul 2015 13:14:25 -0700
parents 42b7d7758c4a
children
comparison
equal deleted inserted replaced
20442:4bde15a9c8bb 20443:71bd638e0ef6
592 ## of "fontsize" property 592 ## of "fontsize" property
593 proplist = {"fontunits", "fontangle", "fontname", "fontsize", ... 593 proplist = {"fontunits", "fontangle", "fontname", "fontsize", ...
594 "fontweight"}; 594 "fontweight"};
595 ca_props = get (ca(1), proplist); 595 ca_props = get (ca(1), proplist);
596 set (hlegend, proplist, ca_props); 596 set (hlegend, proplist, ca_props);
597 old_hplots = [];
597 else 598 else
598 addprops = false; 599 addprops = false;
599 axes (hlegend); 600 axes (hlegend);
600 delete (get (hlegend, "children")); 601 delete (get (hlegend, "children"));
602 ## Hack, to get list of hplots for which addlistener has already been called.
603 old_hplots = [ get(hlegend, "deletefcn"){6:end} ];
601 endif 604 endif
602 if (addprops) 605 if (addprops)
603 addproperty ("edgecolor", hlegend, "color", [0, 0, 0]); 606 addproperty ("edgecolor", hlegend, "color", [0, 0, 0]);
604 addproperty ("textcolor", hlegend, "color", [0, 0, 0]); 607 addproperty ("textcolor", hlegend, "color", [0, 0, 0]);
605 locations = {"north", "south", "east", "west", ... 608 locations = {"north", "south", "east", "west", ...
857 "markersize", min (get (hplots(k), "markersize"),10), 860 "markersize", min (get (hplots(k), "markersize"),10),
858 "userdata", hplots(k)); 861 "userdata", hplots(k));
859 hobjects(end+1) = l1; 862 hobjects(end+1) = l1;
860 endif 863 endif
861 864
862 if (addprops) 865 if (! any (hplots(k) == old_hplots))
863 addlistener (hplots(k), "color", 866 addlistener (hplots(k), "color",
864 {@updateline, hlegend, linelength, false}); 867 {@updateline, hlegend, linelength, false});
865 addlistener (hplots(k), "linestyle", 868 addlistener (hplots(k), "linestyle",
866 {@updateline, hlegend, linelength, false}); 869 {@updateline, hlegend, linelength, false});
867 addlistener (hplots(k), "linewidth", 870 addlistener (hplots(k), "linewidth",
1252 %! set (h, 'fontsize', 20); 1255 %! set (h, 'fontsize', 20);
1253 1256
1254 %!demo 1257 %!demo
1255 %! clf; 1258 %! clf;
1256 %! plot (rand (3)); 1259 %! plot (rand (3));
1257 %! title ('legend("show") without inputs creates default labels'); 1260 %! title ('legend ("show") without inputs creates default labels');
1258 %! h = legend ('show'); 1261 %! h = legend ('show');
1259 1262
1260 %!demo 1263 %!demo
1261 %! clf; 1264 %! clf;
1262 %! x = 0:1; 1265 %! x = 0:1;
1338 %! clf; 1341 %! clf;
1339 %! plot (rand (2)); 1342 %! plot (rand (2));
1340 %! title ('Labels with TeX interpreter turned off'); 1343 %! title ('Labels with TeX interpreter turned off');
1341 %! h = legend ('Hello_World', 'foo^bar'); 1344 %! h = legend ('Hello_World', 'foo^bar');
1342 %! set (h, 'interpreter', 'none'); 1345 %! set (h, 'interpreter', 'none');
1343
1344 %!demo
1345 %! clf;
1346 %! plot (1:10, 1:10);
1347 %! title ('a very long label can sometimes cause problems');
1348 %! legend ('hello very big world', 'location', 'northeastoutside');
1349 1346
1350 %!demo 1347 %!demo
1351 %! clf; 1348 %! clf;
1352 %! labels = {}; 1349 %! labels = {};
1353 %! colororder = get (gca, 'colororder'); 1350 %! colororder = get (gca, 'colororder');
1394 %! title ('legend() works for bar graphs (hggroups)'); 1391 %! title ('legend() works for bar graphs (hggroups)');
1395 %! legend ({'1st Bar', '2nd Bar', '3rd Bar'}); 1392 %! legend ({'1st Bar', '2nd Bar', '3rd Bar'});
1396 1393
1397 %!demo 1394 %!demo
1398 %! clf; 1395 %! clf;
1396 %! colormap (cool (64));
1397 %! surf (peaks ());
1398 %! legend ('peaks()')
1399 %! title ('legend() works for surface objects too');
1400
1401 %!demo
1402 %! clf reset; % needed to undo colormap assignment in previous demo
1399 %! rand_2x3_data2 = [0.44804, 0.84368, 0.23012; 0.72311, 0.58335, 0.90531]; 1403 %! rand_2x3_data2 = [0.44804, 0.84368, 0.23012; 0.72311, 0.58335, 0.90531];
1400 %! bar (rand_2x3_data2); 1404 %! bar (rand_2x3_data2);
1401 %! ylim ([0 1.2]); 1405 %! ylim ([0 1.2]);
1402 %! title ('"left" option places text label west of colors'); 1406 %! title ('"left" option places text label west of colors');
1403 %! legend ('1st Bar', '2nd Bar', '3rd Bar'); 1407 %! legend ('1st Bar', '2nd Bar', '3rd Bar');
1453 %! plot (x, rand (numel (x))); 1457 %! plot (x, rand (numel (x)));
1454 %! legend (cellstr (num2str (x)), 'location', 'southwestoutside'); 1458 %! legend (cellstr (num2str (x)), 'location', 'southwestoutside');
1455 %! subplot (2,2,4); 1459 %! subplot (2,2,4);
1456 %! plot (x, rand (numel (x))); 1460 %! plot (x, rand (numel (x)));
1457 %! legend (cellstr (num2str (x)), 'location', 'southeastoutside'); 1461 %! legend (cellstr (num2str (x)), 'location', 'southeastoutside');
1462 %! %% Legend works on a per axes basis for each subplot
1458 1463
1459 %!demo 1464 %!demo
1460 %! clf; 1465 %! clf;
1461 %! plot (rand (2)); 1466 %! plot (rand (2));
1462 %! title ('legend() will warn if extra labels are specified'); 1467 %! title ('legend() will warn if extra labels are specified');
1487 %! y2 = rand (size (x)); 1492 %! y2 = rand (size (x));
1488 %! [ax, h1, h2] = plotyy (x, y1, x, y2); 1493 %! [ax, h1, h2] = plotyy (x, y1, x, y2);
1489 %! title ('plotyy legend test #3: Blue and Green labels'); 1494 %! title ('plotyy legend test #3: Blue and Green labels');
1490 %! legend ('Blue', 'Green', 'location', 'south'); 1495 %! legend ('Blue', 'Green', 'location', 'south');
1491 1496
1497 %!demo
1498 %! clf;
1499 %! plot (1:10, 1:10);
1500 %! title ('a very long label can sometimes cause problems');
1501 %! legend ('hello very big world', 'location', 'northeastoutside');
1502
1492 %!demo % bug 36408 1503 %!demo % bug 36408
1493 %! clf; 1504 %! clf;
1494 %! option = 'right'; 1505 %! option = 'right';
1495 %! subplot (3,1,1); 1506 %! subplot (3,1,1);
1496 %! plot (rand (1,4)); 1507 %! plot (rand (1,4));
1497 %! xlabel xlabel; 1508 %! xlabel xlabel;
1498 %! ylabel ylabel; 1509 %! ylabel ylabel;
1499 %! title ('Subplots should adjust to the legend placed outside'); 1510 %! title ('Subplots adjust to the legend placed outside');
1500 %! legend ({'1'}, 'location', 'northeastoutside'); 1511 %! legend ({'1'}, 'location', 'northeastoutside');
1501 %! legend (option); 1512 %! legend (option);
1502 %! subplot (3,1,2); 1513 %! subplot (3,1,2);
1503 %! plot (rand (1,4)); 1514 %! plot (rand (1,4));
1504 %! xlabel xlabel; 1515 %! xlabel xlabel;
1515 %!demo % bug 36408 1526 %!demo % bug 36408
1516 %! clf; 1527 %! clf;
1517 %! option = 'right'; 1528 %! option = 'right';
1518 %! subplot (3,1,1); 1529 %! subplot (3,1,1);
1519 %! plot (rand (1,4)); 1530 %! plot (rand (1,4));
1520 %! title ('Subplots should adjust to the legend placed outside'); 1531 %! title ('Subplots adjust to the legend placed outside');
1521 %! legend ({'1'}, 'location', 'northwestoutside'); 1532 %! legend ({'1'}, 'location', 'northwestoutside');
1522 %! legend (option); 1533 %! legend (option);
1523 %! subplot (3,1,2); 1534 %! subplot (3,1,2);
1524 %! plot (rand (1,4)); 1535 %! plot (rand (1,4));
1525 %! legend ({'1234567890'}, 'location', 'westoutside'); 1536 %! legend ({'1234567890'}, 'location', 'westoutside');
1535 %! subplot (3,1,1); 1546 %! subplot (3,1,1);
1536 %! plot (rand (1,4)); 1547 %! plot (rand (1,4));
1537 %! set (gca (), 'yaxislocation', 'right'); 1548 %! set (gca (), 'yaxislocation', 'right');
1538 %! xlabel ('xlabel'); 1549 %! xlabel ('xlabel');
1539 %! ylabel ('ylabel'); 1550 %! ylabel ('ylabel');
1540 %! title ('Subplots should adjust to the legend placed outside'); 1551 %! title ('Subplots adjust to the legend placed outside');
1541 %! legend ({'1'}, 'location', 'northeastoutside'); 1552 %! legend ({'1'}, 'location', 'northeastoutside');
1542 %! legend (option); 1553 %! legend (option);
1543 %! subplot (3,1,2); 1554 %! subplot (3,1,2);
1544 %! plot (rand (1,4)); 1555 %! plot (rand (1,4));
1545 %! set (gca (), 'yaxislocation', 'right'); 1556 %! set (gca (), 'yaxislocation', 'right');
1561 %! subplot (3,1,1); 1572 %! subplot (3,1,1);
1562 %! plot (rand (1,4)); 1573 %! plot (rand (1,4));
1563 %! set (gca (), 'yaxislocation', 'right'); 1574 %! set (gca (), 'yaxislocation', 'right');
1564 %! xlabel ('xlabel'); 1575 %! xlabel ('xlabel');
1565 %! ylabel ('ylabel'); 1576 %! ylabel ('ylabel');
1566 %! title ('Subplots should adjust to the legend placed outside'); 1577 %! title ('Subplots adjust to the legend placed outside');
1567 %! legend ({'1'}, 'location', 'northwestoutside'); 1578 %! legend ({'1'}, 'location', 'northwestoutside');
1568 %! legend (option); 1579 %! legend (option);
1569 %! subplot (3,1,2); 1580 %! subplot (3,1,2);
1570 %! plot (rand (1,4)); 1581 %! plot (rand (1,4));
1571 %! set (gca (), 'yaxislocation', 'right'); 1582 %! set (gca (), 'yaxislocation', 'right');
1587 %! subplot (3,1,1); 1598 %! subplot (3,1,1);
1588 %! plot (rand (1,4)); 1599 %! plot (rand (1,4));
1589 %! set (gca (), 'xaxislocation', 'top'); 1600 %! set (gca (), 'xaxislocation', 'top');
1590 %! xlabel ('xlabel'); 1601 %! xlabel ('xlabel');
1591 %! ylabel ('ylabel'); 1602 %! ylabel ('ylabel');
1592 %! title ('Subplots should adjust to the legend placed outside'); 1603 %! title ('Subplots adjust to the legend placed outside');
1593 %! legend ({'1'}, 'location', 'northwestoutside'); 1604 %! legend ({'1'}, 'location', 'northwestoutside');
1594 %! legend (option); 1605 %! legend (option);
1595 %! subplot (3,1,2); 1606 %! subplot (3,1,2);
1596 %! plot (rand (1,4)); 1607 %! plot (rand (1,4));
1597 %! set (gca (), 'xaxislocation', 'top'); 1608 %! set (gca (), 'xaxislocation', 'top');
1609 1620
1610 %!demo % bug 39697 1621 %!demo % bug 39697
1611 %! clf; 1622 %! clf;
1612 %! plot (1:10); 1623 %! plot (1:10);
1613 %! legend ('Legend Text'); 1624 %! legend ('Legend Text');
1614 %! title ({'Multi-line', 'titles', 'are a', 'problem'}); 1625 %! title ({'Multi-line', 'titles', 'are a', 'problem', 'See bug #39697'});
1615
1616 %!demo
1617 %! clf;
1618 %! colormap (cool (64));
1619 %! surf (peaks ());
1620 %! legend ('peaks()')
1621 %! title ('legend() works for surface objects too');
1622 1626
1623 %!test 1627 %!test
1624 %! toolkit = graphics_toolkit ("gnuplot"); 1628 %! toolkit = graphics_toolkit ("gnuplot");
1625 %! h = figure ("visible", "off"); 1629 %! h = figure ("visible", "off");
1626 %! unwind_protect 1630 %! unwind_protect